我正在尝试循环遍历一个数组,该数组包含一个字符串,其中有几个由空格分隔的单词。最终我想让字符串中的每个单词成为一个新变量,这样我就可以将它与其他变量进行比较。
我的方法是循环将数组的索引分配给另一个数组,直到我遇到一个空格,然后它开始一个新的循环,从另一个数组再次钓鱼的地方开始存储到另一个数组。这似乎对我不起作用。
最简单的方法是什么?我对此很陌生,所以越简单越好。我已附上迄今为止我尝试过的内容。
#include <stdio.h>
int main(){
char input[100];
char order1[20];
char order2[20];
char order3[20];
char order4[20];
char space = ' ';
int counter = 0;
//input is an order that is separated by spaces
fgets(input, 100, stdin);
printf("The order is: %s \n", input);
for(int i; i<20; i++){
printf("%c \n", input[i]);
}
for(int i=counter; i<100; i++) {
if(input[i] == space){
break;
}
else {
order1[12] += input[i];
}
}
for(int j=counter; j<100; j++) {
if(input[j] == space){
break;
}
else {
order2[12] += input[j];
}
}
for(int k=counter; k<100; k++) {
if(input[k] == space){
break;
}
else {
order3[12] += input[k];
}
}
for(int l=counter; l<100; l++) {
if(input[l] == space){
break;
}
else {
order4[12] += input[l];
}
}
printf("Order 1: %s \n", order1);
printf("Order 2: %s \n", order2);
printf("Order 3: %s \n", order3);
printf("Order 4: %s", order4);
return 0;
return 0;
}
如果我理解正确,问题是在空格处分割字符串并将这些标记存储在单独的字符串中。
如果是这种情况,人们可能会问以下构造有什么用处:
for (int i=counter; i<100; i++) { //from offset 0 to 100
//since counter is set to 0 and never modified again
if (input[i] == space) { //if input at offset i is space
break; //end loop
} else { //else for every other character in input
order1[12] += input[i]; //increase the value at offset 12 in array order1
//by the value at offset i in input
//(considering that the chars are most likely alphanumeric
// we reach the limit of a char very fast)
}
}
由于您没有对数组进行任何其他操作
order1
(打印除外),问题是 20 个字符中有 19 个未初始化,并且偏移量 12 处的值很可能不是您所期望的。
首先,我们摆脱神奇的数字并定义有意义的符号
#define MAX_INPUT_LENGTH 100 //maximum amount of chars in input
#define MAX_ORDER_LENGTH 20 //maximum amount of chars in an order
char input[MAX_INPUT_LENGTH];
char order1[MAX_ORDER_LENGTH];
char order2[MAX_ORDER_LENGTH];
//...
但是如果我们有一个字符串数组而不是单个变量会更好,因此
#define MAX_NUM_ORDER 4 //maximum amount of orders
char order[MAX_NUM_ORDER][MAX_ORDER_LENGTH];
我们现在有一个字符数组的数组,或者换句话说,一个订单列表,其中每个订单都有特定的长度。
让我们把它们填满:
//read input
if (fgets(input, MAX_INPUT_LENGTH, stdin) == NULL) {
fputs("read input failed.\n", stderr); //print to error stream
exit(EXIT_FAILURE); //exit program with error status code
}
//at this point, we should have a valid input
printf("The order is: %s \n", input);
char *pos = input; //pointer to first char in input
char *mark = pos; //order token start
int num_order = 0; //number of orders, also slot offset
int done = 0; //loop condition
//for each occurrence of space in input string
do {
//find first occurrence of space
pos = strchr(pos, ' ');
if (pos == NULL) { //not found
//set pos to eol (end of line)
pos = input + strlen(input);
//indicate end of loop
done = 1;
}
//order token length
const size_t order_length = pos - mark;
//test string size limit (excluding one for '\0')
if (order_length < MAX_ORDER_LENGTH) {
//test list size limit
if (num_order < MAX_NUM_ORDER) {
//copy from input into order string
memcpy(order[num_order], mark, order_length);
//make null terminated string
order[num_order][order_length] = '\0';
//increasee the number of orders by one
++num_order;
//set new order token start (skip space)
mark = pos + 1;
} else {
fputs("not able to store more orders.", stderr);
exit(EXIT_FAILURE);
}
} else {
fputs("order is too long.", stderr);
exit(EXIT_FAILURE);
}
} while (!done);
//print all orders
for (int i=0; i < num_order; ++i) {
printf("Order %d: %s\n", i, order[i]);
}