英文:
Separating a long string into individual words in C
问题
Sure, here's the translated code portion:
我尝试做的是,给定一个字符串(比如 "This is a string"),编写一个能够将其拆分成一个数组的代码,每个单词都放在数组的一个单独元素中("This","is","a","string")。我草拟了这样的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='
我尝试做的是,给定一个字符串(比如 "This is a string"),编写一个能够将其拆分成一个数组的代码,每个单词都放在数组的一个单独元素中("This","is","a","string")。我草拟了这样的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='\0') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='
我尝试做的是,给定一个字符串(比如 "This is a string"),编写一个能够将其拆分成一个数组的代码,每个单词都放在数组的一个单独元素中("This","is","a","string")。我草拟了这样的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='\0') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
If you have any further questions or need assistance with the code, please feel free to ask.
英文:
What I'm trying to do is, given a string (say "This is a string") write a code that could take it and put each word in a separate element of an array ("This", "is", "a", "string"). I scribbled something like that:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='\0') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char bigString[1000];
scanf("%s", bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i<10; i++)
{
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]=='\0') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
') i=10;
bigIndex++;
smallIndex=0;
printf("%s", wordCollection[i]);
}
}
Unfortunately, it doesn't act as needed (printing out some gibberish). I tried debugging it but I still don't understand why it wouldn't function properly. Guidance would be very helpful
Also - if you have a more efficient idea, say one involving pointers, I would be more than thrilled to see it.
答案1
得分: 1
Here are the translated portions:
"你没有在 wordCollection
中对单词进行空字符终止。"
在 while
后面添加 wordCollection[i][smallIndex] = '\0';
,例如:
while (bigString[bigIndex] != ' ' && bigString[bigIndex] != '
while (bigString[bigIndex] != ' ' && bigString[bigIndex] != '\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
wordCollection[i][smallIndex] = '\0';
')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
wordCollection[i][smallIndex] = '
while (bigString[bigIndex] != ' ' && bigString[bigIndex] != '\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
wordCollection[i][smallIndex] = '\0';
';
"你还应该检查大小,记住,空字符终止的字符串占用的字符比字符串长度多一个。"
此外,在 printf
之前设置 i=10
会导致最后一个 printf
打印无意义的内容,因为如果少于 10 个单词,你没有放置任何内容。更糟糕的是,它可能会导致分段错误,因为 wordCollection
数组的最后一个索引是 9
。
英文:
You are not null terminating the words in wordCollection
Add a wordCollection[i][smallIndex] = '\0
after the while, e.g.:
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
wordCollection[i][smallIndex] = '\0';
You should also check sizes, remember that a null terminated string occupies one more char than the string length.
Also, setting i=10
before the printf
will make the last printf
print gibberish, because you didn't put anything there if there were less than 10 words. Even worst, it could give a segmentation fault, because the last index of the wordCollection
array is 9
.
答案2
得分: 1
以下是您要翻译的内容:
对于这个scanf
的调用
scanf("%s", bigString);
是不安全的,只能读取一个单词。
相反,您可以使用标准函数fgets
。
如果遇到空格字符,那么这个while循环
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
')
')
会被跳过。在这种情况下,两维字符数组的索引i
不会被字符串填充。因此,这个printf的调用
printf("%s", wordCollection[i]);
会引发未定义行为。而且,如果记录中有多个相邻的空格,且字符串中的单词数少于10个,可能会出现这样的情况,即不会输出任何单词。
此外,您需要将用空字符'\0'
填充的数组wordCollection
的每个元素追加在一起以形成一个字符串。
而且这一对语句
if (bigString[bigIndex]=='
if (bigString[bigIndex]=='
if (bigString[bigIndex]=='\0') i=10;
//...
printf("%s", wordCollection[i]);
') i=10;
//...
printf("%s", wordCollection[i]);
') i=10;
//...
printf("%s", wordCollection[i]);
再次引发未定义行为,因为10是一个无效的索引。
您可以使用标准的C字符串函数代替手动编写的while循环。
并且要注意,通常单词可以由多个相邻的空格或制表符分隔开。
以下是演示程序。
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
该程序的输出是
This is a string
This
is
a
string
英文:
For starters this call of scanf
scanf("%s", bigString);
is unsafe and can read only one word.
Instead you can use standard function fgets
.
If a space character is encountered then this while loop
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='
while (bigString[bigIndex]!=' ' && bigString[bigIndex]!='\0')
')
')
is skipped. In this case an element with the index i
of the two-dimensional character array will not be filled by a string. As a result this call of printf
printf("%s", wordCollection[i]);
invokes undefined behavior. And moreover if there are several adjacent spaces in the record and the number of words in the string is even less than 10 it can occur such a way that neither word will be outputted.
Also you need to append each element of the array wordCollection
that was filled with the terminating zero character '\0'
to make a string.
And this pair of statements
if (bigString[bigIndex]=='
if (bigString[bigIndex]=='
if (bigString[bigIndex]=='\0') i=10;
//...
printf("%s", wordCollection[i]);
') i=10;
//...
printf("%s", wordCollection[i]);
') i=10;
//...
printf("%s", wordCollection[i]);
again invokes undefined behavior because 10 is an invalid index.
Instead of the manually written while loop you could use standard C string functions.
And pay attention to that in general words can be separated by several adjacent spaces of tab characters.
Here is a demonstration program.
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
const char *delim = " \t";
for (const char *p = bigString; *p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { MAX_STRING_LEN = 1000, WORD_COUNT = 10, MAX_WORD_LEN = 100 };
char bigString[MAX_STRING_LEN];
char wordCollection[WORD_COUNT][MAX_WORD_LEN];
size_t count = 0;
if (fgets( bigString, MAX_STRING_LEN, stdin ) != NULL)
{
bigString[strcspn( bigString, "\n" )] = '\0';
const char *delim = " \t";
for (const char *p = bigString; *p != '\0' && count < WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != '\0')
{
size_t n = strcspn( p, delim );
size_t len = n < MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = '\0';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
';
++count;
p += n;
}
}
for (size_t i = 0; i < count; i++)
{
puts( wordCollection[i] );
}
}
}
The program output is
This is a string
This
is
a
string
答案3
得分: 0
- 使用符号常量而不是魔法值。
scanf("%s", ...)
读取以空格分隔的单词,因此bigString
永远不会包含多于1个单词。你可以使用"%[^\n]"
格式字符串来读取行(但请看下一个)。- 使用
scanf()
读取字符串时,始终使用最大字段宽度。 - 检查
scanf()
的返回值,否则你可能会操作未初始化的数据。 - 我建议你使用
fgets()
而不是scanf()
。 - 我建议你使用单循环而不是双循环。这意味着你只需在一个地方进行边界检查。
- (未修复)考虑将打印与处理分开;如果这样做,
bigString
的终止'\0'
就是正常情况,而break
可以移到循环底部。 - 你需要以
'\0'
终止单词;否则printf()
将是未定义的行为。 i=10;
后跟wordCollection[i]
超出了边界,这意味着未定义的行为。
英文:
- Use symbolic constants instead of magic values.
scanf("%s", ...)
reads white space separated words sobigString
will never contain more than 1 word. You can use a "%[^\n]" the format string to read lines (but see next).- Always use a maximum field width when reading strings with
scanf()
. - Check the return value from
scanf()
otherwise you might be operating on uninitialized data. - I suggest you use
fgets()
instead ofscanf()
. - I suggest you use a single loop instead of the double loop. This means you only have to do boundary checks in one place.
- (Not fixed) Consider separating print from processing; if you do then the terminating
'\0'
ofbigString
is just the normal case, and the break can move to the bottom of the loop. - You need to '\0' terminate words; otherwise
printf()
will be undefined behavior. i=10;
followed bywordCollection[i]
is out of bounds which means undefined behavior.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 1000
#define WORDS 10
#define WORD_LEN 100
int main(void) {
char bigString[STRING_LEN];
if(!fgets(bigString, STRING_LEN, stdin)) {
printf("fgets() failed\n");
return 1;
}
bigString[strcspn(bigString, "\n")] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 1000
#define WORDS 10
#define WORD_LEN 100
int main(void) {
char bigString[STRING_LEN];
if(!fgets(bigString, STRING_LEN, stdin)) {
printf("fgets() failed\n");
return 1;
}
bigString[strcspn(bigString, "\n")] = '\0';
char wordCollection[WORDS][WORD_LEN];
int word = 0;
int word_i = 0;
for(int i = 0;; i++) {
if(word_i == WORD_LEN) {
printf("word too big\n");
return 1;
}
if(word == WORDS) {
printf("too many words\n");
return 1;
}
if(bigString[i] == ' ' || !bigString[i]) {
wordCollection[word][word_i] = '\0';
printf("%s\n", wordCollection[word]);
word++;
word_i = 0;
if(!bigString[i])
break;
continue;
}
wordCollection[word][word_i++] = bigString[i];
}
}
';
char wordCollection[WORDS][WORD_LEN];
int word = 0;
int word_i = 0;
for(int i = 0;; i++) {
if(word_i == WORD_LEN) {
printf("word too big\n");
return 1;
}
if(word == WORDS) {
printf("too many words\n");
return 1;
}
if(bigString[i] == ' ' || !bigString[i]) {
wordCollection[word][word_i] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 1000
#define WORDS 10
#define WORD_LEN 100
int main(void) {
char bigString[STRING_LEN];
if(!fgets(bigString, STRING_LEN, stdin)) {
printf("fgets() failed\n");
return 1;
}
bigString[strcspn(bigString, "\n")] = '\0';
char wordCollection[WORDS][WORD_LEN];
int word = 0;
int word_i = 0;
for(int i = 0;; i++) {
if(word_i == WORD_LEN) {
printf("word too big\n");
return 1;
}
if(word == WORDS) {
printf("too many words\n");
return 1;
}
if(bigString[i] == ' ' || !bigString[i]) {
wordCollection[word][word_i] = '\0';
printf("%s\n", wordCollection[word]);
word++;
word_i = 0;
if(!bigString[i])
break;
continue;
}
wordCollection[word][word_i++] = bigString[i];
}
}
';
printf("%s\n", wordCollection[word]);
word++;
word_i = 0;
if(!bigString[i])
break;
continue;
}
wordCollection[word][word_i++] = bigString[i];
}
}
example run:
hello world
hello
world
1 2 3 4 5 6 7 8 9 10 11
1
2
3
4
5
6
7
8
9
10
too many words
and with WORD_LEN reduced to 10 (not enough space to write the terminating '\0'):
1234567890
word too big
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论