在C中遍历字符串并用空格分隔输入
我有一个字符串,比如“first second”,我想要的结果是这个输出:在C中遍历字符串并用空格分隔输入,c,string,loops,pointers,C,String,Loops,Pointers,我有一个字符串,比如“first second”,我想要的结果是这个输出: first second 但我得到的结果是: first first second 我知道我的update语句或创建子字符串时存在问题。如果有人能帮我,那就太好了。下面是我的代码: int counter = 0; //counter used in loop int index = test->current_index; //holds the current index of my string, it
first
second
但我得到的结果是:
first
first second
我知道我的update语句或创建子字符串时存在问题。如果有人能帮我,那就太好了。下面是我的代码:
int counter = 0; //counter used in loop
int index = test->current_index; //holds the current index of my string, it's initially 0
char *string = test->myString; //holds the whole string
char token_buffer = string[index];
//before loop: index = 0, counter = 0
while(test->current_index <= test->end_index) //test->end_index holds last index of string
{
while(token_buffer != ' ')
{
counter++;
token_buffer = string[index + counter];
}
char *output_token = malloc(counter+1);
strncpy( output_token, string, counter );
//printing token
printf("%s \n", output_token);
//update loop (possible problem area!)
test->current_index += counter;
index += counter;
token_buffer+=string[counter];
counter =0;
}
return 0;
}
int计数器=0//循环中使用的计数器
int index=测试->当前索引//保存字符串的当前索引,最初为0
char*string=test->myString//控制整个弦
char token_buffer=字符串[索引];
//循环前:索引=0,计数器=0
而(test->current_index end_index)//test->end_index保存字符串的最后一个索引
{
while(令牌缓冲区!='')
{
计数器++;
令牌缓冲区=字符串[索引+计数器];
}
char*output_token=malloc(计数器+1);
strncpy(输出令牌、字符串、计数器);
//打印令牌
printf(“%s\n”,输出\u令牌);
//更新循环(可能的问题区域!)
测试->当前_索引+=计数器;
指数+=计数器;
令牌缓冲区+=字符串[计数器];
计数器=0;
}
返回0;
}
问题似乎在于调用strncpy
。
与:
您正在从output\u token
的string
开头复制第一个计数器
字符。要调用strncpy
最常用的方法是将源字符串移动到current\u index
。比如:
strncpy( output_token, (char*)(string+test->current_index), counter );
在循环结束时。问题似乎是调用了
strncpy
。
与:
您正在从output\u token
的string
开头复制第一个计数器
字符。要调用strncpy
最常用的方法是将源字符串移动到current\u index
。比如:
strncpy( output_token, (char*)(string+test->current_index), counter );
在循环结束时。问题似乎是调用了
strncpy
。
与:
您正在从output\u token
的string
开头复制第一个计数器
字符。要调用strncpy
最常用的方法是将源字符串移动到current\u index
。比如:
strncpy( output_token, (char*)(string+test->current_index), counter );
在循环结束时。问题似乎是调用了
strncpy
。
与:
您正在从output\u token
的string
开头复制第一个计数器
字符。要调用strncpy
最常用的方法是将源字符串移动到current\u index
。比如:
strncpy( output_token, (char*)(string+test->current_index), counter );
循环结束时。代码中有两个错误:
strncpy
中,每次复制到output\u令牌时,您都开始从字符串的起始地址进行复制
while
循环条件token\u buffer!=''中代码>。因为在字符串的末尾没有空格,所以计数器将继续增加并超出test->end\u索引
,直到它读取空格为止
counter = 0;
token_buffer = string[counter+index];
while(token_buffer != ' ' && (index+counter)<=test->end_index) //since counter is always set to zero before this loop executes.
{
counter++;
token_buffer = string[index+counter];
}
char *output_token = calloc(counter+1);
strncpy(output_token,string+index,counter);
printf("%s \n", output_token);
counter++; //This is required since when the loop exit, counter would be at the position of ' '.
test->current_index += counter;
index += counter;
计数器=0;
令牌缓冲区=字符串[计数器+索引];
while(token_buffer!=''&&(index+counter)end_index)//因为在执行此循环之前,计数器总是设置为零。
{
计数器++;
令牌缓冲区=字符串[索引+计数器];
}
char*output_token=calloc(计数器+1);
strncpy(输出令牌、字符串+索引、计数器);
printf(“%s\n”,输出\u令牌);
计数器++//这是必需的,因为当循环退出时,计数器将位于“”的位置。
测试->当前_索引+=计数器;
指数+=计数器;
将其添加到主while循环中。代码中有两个错误:
strncpy
中,每次复制到output\u令牌时,您都开始从字符串的起始地址进行复制
while
循环条件token\u buffer!=''中代码>。因为在字符串的末尾没有空格,所以计数器将继续增加并超出test->end\u索引
,直到它读取空格为止
counter = 0;
token_buffer = string[counter+index];
while(token_buffer != ' ' && (index+counter)<=test->end_index) //since counter is always set to zero before this loop executes.
{
counter++;
token_buffer = string[index+counter];
}
char *output_token = calloc(counter+1);
strncpy(output_token,string+index,counter);
printf("%s \n", output_token);
counter++; //This is required since when the loop exit, counter would be at the position of ' '.
test->current_index += counter;
index += counter;
计数器=0;
令牌缓冲区=字符串[计数器+索引];
while(token_buffer!=''&&(index+counter)end_index)//因为在执行此循环之前,计数器总是设置为零。
{
计数器++;
令牌缓冲区=字符串[索引+计数器];
}
char*output_token=calloc(计数器+1);
strncpy(输出令牌、字符串+索引、计数器);
printf(“%s\n”,输出\u令牌);
计数器++//这是必需的,因为当循环退出时,计数器将位于“”的位置。
测试->当前_索引+=计数器;
指数+=计数器;
将其添加到主while循环中。代码中有两个错误:
strncpy
中,每次复制到output\u令牌时,您都开始从字符串的起始地址进行复制
while
循环条件token\u buffer!=''中代码>。因为在字符串的末尾没有空格,所以计数器将继续增加并超出test->end\u索引
,直到它读取空格为止
counter = 0;
token_buffer = string[counter+index];
while(token_buffer != ' ' && (index+counter)<=test->end_index) //since counter is always set to zero before this loop executes.
{
counter++;
token_buffer = string[index+counter];
}
char *output_token = calloc(counter+1);
strncpy(output_token,string+index,counter);
printf("%s \n", output_token);
counter++; //This is required since when the loop exit, counter would be at the position of ' '.
test->current_index += counter;
index += counter;
计数器=0;
令牌缓冲区=字符串[计数器+索引];
while(token_buffer!=''&&(index+counter)end_index)//因为在执行此循环之前,计数器总是设置为零。
{
计数器++;
令牌缓冲区=字符串[索引+计数器];
}
char*output_token=calloc(计数器+1);
strncpy(输出令牌、字符串+索引、计数器);
printf(“%s\n”,输出\u令牌);
计数器++//这是必需的,因为当循环退出时,计数器将位于“”的位置。
测试->当前_索引+=计数器;
指数+=计数器;
将其添加到主while循环中。代码中有两个错误:
strncpy
中,每次复制到output\u令牌时,您都开始从字符串的起始地址进行复制
while
循环条件token\u buffer!=''中代码>。因为在字符串的末尾,你没有空格,所以你的计数器会一直在里面