无法在 while 循环中将空格附加到字符串

Unable to append a whitespace to a string in while loop

下面是我的代码,它的作用是将输入输入到变量 string1 中,然后我的 while 循环标记每个单词,我能够达到我的任务目标,即反转一个单词中的单词细绳。在我的代码下方是它的输出,如您所见,没有 spaces。如何将白色 space 放入与原始字符串相似的最终字符串中?

代码:

int main()
{
  int ch, ss=0, s=0;
  char x[3];
  char *word, string1[100], string2[100], temp[100]={0};
  x[0]='y';

  while(x[0]=='y'||x[0]=='Y')
  {
    printf("Enter a String: ");
    fgets(string1, 100, stdin);
    if (string1[98] != '\n' && string1[99] == '[=10=]') { while ( (ch = fgetc(stdin)) != EOF && ch != '\n'); }

    word = strtok(string1, " \n");

    while(word != NULL)
    {
      s = strlen(word);
      ss=ss+s;

      strncpy(string2, word, s+1);
      strncat(string2, temp, ss);
      strncpy(temp, string2, ss+1);

      printf("string2: %s\n",temp);     
      word = strtok(NULL, " \n");
    }




    printf("Run Again?(y/n):");
    fgets(x, 2, stdin);
    while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
  } 
  return 0;
}

输出:

Enter a String: AAA BBB CCC
string2: AAA  
string2: BBBAAA   
string2: CCCBBBAAA

简单地添加空格怎么样?

#include <stdio.h>
#include <string.h>

int main()
{
  int ch, ss=0, s=0;
  char x[3];
  char *word, string1[100]={0}, string2[200], temp[200]={0};
  x[0]='y';

  while(x[0]=='y'||x[0]=='Y')
  {
    printf("Enter a String: ");
    fgets(string1, 100, stdin);
    if (string1[98] != '\n' && string1[99] == '[=10=]') { while ( (ch = fgetc(stdin)) != EOF && ch != '\n'); }

    word = strtok(string1, " \n");

    while(word != NULL)
    {
      s = strlen(word);
      ss=ss+s;

      /* copy the word to the working buffer */
      strncpy(string2, word, s+1);
      /* if this is not the first word, put a space after the word */
      if (temp[0] != '[=10=]') strcat(string2, " "); /* add this */
      /* concat the previously read words after the new word and a space */
      strncat(string2, temp, ss);
      /* adjust the length of the string for the added space */
      ss++; /* add this for space */
      /* copy the new result to temp */
      strncpy(temp, string2, ss+1);

      printf("string2: %s\n",temp);
      word = strtok(NULL, " \n");
    }




    printf("Run Again?(y/n):");
    fgets(x, 2, stdin);
    while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
  } 
  return 0;
}