在C中将长字符串分割为单词

huangapple go评论61阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
int main()
{
  char bigString[1000];
  scanf(&quot;%s&quot;, bigString);
  char wordCollection[10][100];
  int bigIndex=0;
  int smallIndex=0;
  for (int i = 0; i&lt;10; i++)
  {
      while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
int main()
{
char bigString[1000];
scanf(&quot;%s&quot;, bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i&lt;10; i++)
{
while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;\0&#39;)
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]==&#39;\0&#39;) i=10;
bigIndex++;
smallIndex=0;
printf(&quot;%s&quot;, wordCollection[i]);
}
}
&#39;) { wordCollection[i][smallIndex] = bigString[bigIndex]; bigIndex++; smallIndex++; } if (bigString[bigIndex]==&#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
int main()
{
char bigString[1000];
scanf(&quot;%s&quot;, bigString);
char wordCollection[10][100];
int bigIndex=0;
int smallIndex=0;
for (int i = 0; i&lt;10; i++)
{
while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;\0&#39;)
{
wordCollection[i][smallIndex] = bigString[bigIndex];
bigIndex++;
smallIndex++;
}
if (bigString[bigIndex]==&#39;\0&#39;) i=10;
bigIndex++;
smallIndex=0;
printf(&quot;%s&quot;, wordCollection[i]);
}
}
&#39;) i=10; bigIndex++; smallIndex=0; printf(&quot;%s&quot;, 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 在C中将长字符串分割为单词
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] = &#39;\0 after the while, e.g.:

      while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;\0&#39;)
      {
          wordCollection[i][smallIndex] = bigString[bigIndex];
          bigIndex++;
          smallIndex++;
      }
      wordCollection[i][smallIndex] = &#39;\0&#39;;

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(&quot;%s&quot;, bigString);

是不安全的,只能读取一个单词。

相反,您可以使用标准函数fgets

如果遇到空格字符,那么这个while循环

while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;
while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;
while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;\0&#39;)
&#39;)
&#39;)

会被跳过。在这种情况下,两维字符数组的索引i不会被字符串填充。因此,这个printf的调用

printf(&quot;%s&quot;, wordCollection[i]);

会引发未定义行为。而且,如果记录中有多个相邻的空格,且字符串中的单词数少于10个,可能会出现这样的情况,即不会输出任何单词。

此外,您需要将用空字符&#39;\0&#39;填充的数组wordCollection的每个元素追加在一起以形成一个字符串。

而且这一对语句

  if (bigString[bigIndex]==&#39;
  if (bigString[bigIndex]==&#39;
  if (bigString[bigIndex]==&#39;\0&#39;) i=10;
//...
printf(&quot;%s&quot;, wordCollection[i]);
&#39;) i=10; //... printf(&quot;%s&quot;, wordCollection[i]);
&#39;) i=10; //... printf(&quot;%s&quot;, wordCollection[i]);

再次引发未定义行为,因为10是一个无效的索引。

您可以使用标准的C字符串函数代替手动编写的while循环。

并且要注意,通常单词可以由多个相邻的空格或制表符分隔开。

以下是演示程序。

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }

该程序的输出是

This is a string
This
is
a
string
英文:

For starters this call of scanf

scanf(&quot;%s&quot;, 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]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;
while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;
while (bigString[bigIndex]!=&#39; &#39; &amp;&amp; bigString[bigIndex]!=&#39;\0&#39;)
&#39;)
&#39;)

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(&quot;%s&quot;, 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 &#39;\0&#39; to make a string.

And this pair of statements

  if (bigString[bigIndex]==&#39;
  if (bigString[bigIndex]==&#39;
  if (bigString[bigIndex]==&#39;\0&#39;) i=10;
//...
printf(&quot;%s&quot;, wordCollection[i]);
&#39;) i=10; //... printf(&quot;%s&quot;, wordCollection[i]);
&#39;) i=10; //... printf(&quot;%s&quot;, 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 &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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, &quot;\n&quot; )] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; const char *delim = &quot; \t&quot;; for (const char *p = bigString; *p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39; &amp;&amp; count &lt; WORD_COUNT; ) { p += strspn( p, delim ); if (*p != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;) { size_t n = strcspn( p, delim ); size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1; memcpy( wordCollection[count], p, len ); wordCollection[count][len] = &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
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, &quot;\n&quot; )] = &#39;\0&#39;;
const char *delim = &quot; \t&quot;;
for (const char *p = bigString; *p != &#39;\0&#39; &amp;&amp; count &lt; WORD_COUNT; )
{
p += strspn( p, delim );
if (*p != &#39;\0&#39;)
{
size_t n = strcspn( p, delim );
size_t len = n &lt; MAX_WORD_LEN ? n : MAX_WORD_LEN - 1;
memcpy( wordCollection[count], p, len );
wordCollection[count][len] = &#39;\0&#39;;
++count;
p += n;
}
}
for (size_t i = 0; i &lt; count; i++)
{
puts( wordCollection[i] );
}
}
}
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }
&#39;; ++count; p += n; } } for (size_t i = 0; i &lt; count; i++) { puts( wordCollection[i] ); } } }

The program output is

This is a string
This
is
a
string

答案3

得分: 0

  1. 使用符号常量而不是魔法值。
  2. scanf("%s", ...) 读取以空格分隔的单词,因此 bigString 永远不会包含多于1个单词。你可以使用"%[^\n]"格式字符串来读取行(但请看下一个)。
  3. 使用 scanf() 读取字符串时,始终使用最大字段宽度。
  4. 检查 scanf() 的返回值,否则你可能会操作未初始化的数据。
  5. 我建议你使用 fgets() 而不是 scanf()
  6. 我建议你使用单循环而不是双循环。这意味着你只需在一个地方进行边界检查。
  7. (未修复)考虑将打印与处理分开;如果这样做,bigString 的终止'\0'就是正常情况,而 break 可以移到循环底部。
  8. 你需要以'\0'终止单词;否则 printf() 将是未定义的行为。
  9. i=10; 后跟 wordCollection[i] 超出了边界,这意味着未定义的行为。
英文:
  1. Use symbolic constants instead of magic values.
  2. scanf(&quot;%s&quot;, ...) reads white space separated words so bigString will never contain more than 1 word. You can use a "%[^\n]" the format string to read lines (but see next).
  3. Always use a maximum field width when reading strings with scanf().
  4. Check the return value from scanf() otherwise you might be operating on uninitialized data.
  5. I suggest you use fgets() instead of scanf().
  6. I suggest you use a single loop instead of the double loop. This means you only have to do boundary checks in one place.
  7. (Not fixed) Consider separating print from processing; if you do then the terminating &#39;\0&#39; of bigString is just the normal case, and the break can move to the bottom of the loop.
  8. You need to '\0' terminate words; otherwise printf() will be undefined behavior.
  9. i=10; followed by wordCollection[i] is out of bounds which means undefined behavior.
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#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(&quot;fgets() failed\n&quot;);
         return 1;
    }
	bigString[strcspn(bigString, &quot;\n&quot;)] = &#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#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(&quot;fgets() failed\n&quot;);
return 1;
}
bigString[strcspn(bigString, &quot;\n&quot;)] = &#39;\0&#39;;
char wordCollection[WORDS][WORD_LEN];
int word = 0;
int word_i = 0;
for(int i = 0;; i++) {
if(word_i == WORD_LEN) {
printf(&quot;word too big\n&quot;);
return 1;
}
if(word == WORDS) {
printf(&quot;too many words\n&quot;);
return 1;
}
if(bigString[i] == &#39; &#39; || !bigString[i]) {
wordCollection[word][word_i] = &#39;\0&#39;;
printf(&quot;%s\n&quot;, wordCollection[word]);
word++;
word_i = 0;
if(!bigString[i])
break;
continue;
}
wordCollection[word][word_i++] = bigString[i];
}
}
&#39;; char wordCollection[WORDS][WORD_LEN]; int word = 0; int word_i = 0; for(int i = 0;; i++) { if(word_i == WORD_LEN) { printf(&quot;word too big\n&quot;); return 1; } if(word == WORDS) { printf(&quot;too many words\n&quot;); return 1; } if(bigString[i] == &#39; &#39; || !bigString[i]) { wordCollection[word][word_i] = &#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#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(&quot;fgets() failed\n&quot;);
return 1;
}
bigString[strcspn(bigString, &quot;\n&quot;)] = &#39;\0&#39;;
char wordCollection[WORDS][WORD_LEN];
int word = 0;
int word_i = 0;
for(int i = 0;; i++) {
if(word_i == WORD_LEN) {
printf(&quot;word too big\n&quot;);
return 1;
}
if(word == WORDS) {
printf(&quot;too many words\n&quot;);
return 1;
}
if(bigString[i] == &#39; &#39; || !bigString[i]) {
wordCollection[word][word_i] = &#39;\0&#39;;
printf(&quot;%s\n&quot;, wordCollection[word]);
word++;
word_i = 0;
if(!bigString[i])
break;
continue;
}
wordCollection[word][word_i++] = bigString[i];
}
}
&#39;; printf(&quot;%s\n&quot;, 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

huangapple
  • 本文由 发表于 2023年6月9日 02:16:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定