我希望将我破坏的字符串的部分输入到二维数组中,例如: String: "one day" 数组中的结果: Col1: one Col2: day
问题是,如何用第result2
1 列和result
第 2 列的这两个变量填充数组?
到目前为止,这是我的代码(如您所见,我有一个单独的历史数组和一个用于保存用户输入部分的单独数组):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
int i=0; int j=0; int k=0;
char inputString[100];
char *result=NULL;
char *result2=NULL;
char delims[] = " ";
char historyArray[100][100] = {0};
char historyKey[] = "history";
char *tokenArray[100][100] = {0} ;
//char exitString[] = "exit";
do
{
printf("hshell>");
gets(inputString);
strcpy (historyArray[k], inputString);
k++;
// Break the string into parts
result = strtok(inputString, delims);
while (result!=NULL)
{
result2 = result;
puts(result);
result= strtok(NULL, delims);
for (int count = 0; count < k; count++)
tokenArray[count] = result2;
j++;
}
if (strcmp(inputString,historyKey) == 0)
{
for (i=0; i<k; i++)
{
printf("%d. %s \n",i+1,historyArray[i]);
}
}
else if (strcmp ("exit",inputString) != 0)
{
printf("\nCommand not found \n");
}
}while (strcmp ("exit", inputString) != 0);
return 0;
}