我在 C 的输出中得到未知字符

I'm getting unknown characters in output in C

我正在做这个练习:

Write a program that reverses the words of a sentence like this: My name is John --> John is name My

我写了这个:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int word=0,character=0;
    char input[50];
    char output[50][50];
    int InLength;
    printf("Enter some words:\n");
    fgets(input,50,stdin);
    input[strcspn(input, "\n")] = 0;
    InLength=strlen(input);
    for (int i=0;i<InLength;i++){
        if (input[i]==' '){
            character=0;
            word++;
        }
        else{
        output[word][character]=input[i];
        character++;
        }
    }
    for (word;word>=0;word--){
        printf("%s ",output[word]);
    }
    printf("\n");
}

问题是输出给出了一个奇怪的字符,某些单词旁边有一个问号。例如:

Enter some words:
hello how are you
you���� are how hello

另一个例子:

Enter some words:
first second third hello good
good� hello�� third���� second first

我不知道为什么输出显示这些奇怪的字符。这可能是个愚蠢的问题,但我是初学者。

P.S。抱歉我的英语不好。

数组的元素output声明为

char output[50][50];

不包含字符串,因为您忘记为数组元素中的每个存储的字符序列附加终止零字符 '[=14=]'

但在任何情况下,您的方法都是不正确的,因为句子中的单词之间可以有多个 space 个字符,或者例如一个句子可以从 space 个字符开始。

通常任务是通过以下方式解决的。先把整个句子倒过来,然后把句子中的每个词依次倒过来。

这是一个演示程序。

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

void reverse_n( char s[], size_t n )
{
    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n-i-1];
        s[n-i-1] = c;
    }
}

int main(void) 
{
    enum { N = 100 };
    char input[N];
    input[0] = '[=11=]';
    
    printf( "Enter some words: " );
    
    fgets( input, N, stdin );
    input[ strcspn( input, "\n" ) ] = '[=11=]';     
    
    reverse_n( input, strlen( input ) );
    
    const char *separator = " \t";
    
    for ( char *p = input; *p; )
    {
        p += strspn( p, separator );
        char *q = p;
        
        p += strcspn( p, separator );
        
        reverse_n( q, p - q );
    }
    
    puts( input );
    
    return 0;
}

程序输出可能看起来像

Enter some words: My name is John
John  is name My

此代码解决了上述问题,并且可以处理多个 space 字符或以 space.

开头的句子
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char* begin, char* end)
{
    char temp;
    while (begin < end) {
        temp = *begin;
        *begin++ = *end;
        *end-- = temp;
    }
}

void reverseWords(char* s)
{
    char* word_begin = NULL;

    //  /* temp is for word boundary */
    char* temp = s;

    /*STEP 1 of the above algorithm */
    while (*temp)
    {

        /*This condition is to make sure that the 
          string start with valid character (not
          space) only*/
        if ((word_begin == NULL) &&
                  (*temp != ' '))
        {
            word_begin = temp;
        }
        if (word_begin && ((*(temp + 1) == ' ') ||
            (*(temp + 1) == '[=10=]')))
        {
            reverse(word_begin, temp);
            word_begin = NULL;
        }
        temp++;
    } /* End of while */

    /*STEP 2 of the above algorithm */
    reverse(s, temp - 1);
}


int main()
{
    char input[50];
    printf("Please enter a sentence: ");
    fgets(input,50,stdin);
    input[strcspn(input, "\n")] = 0;
    reverseWords(input);
    char* temp = input;
    printf("%s", input);
   return 0;
}

您必须在每个字符串的末尾添加零:

int main(){
    int word=0,character=0;
    char input[50];
    char output[50][50];
    int InLength;
    printf("Enter some words:\n");
    fgets(input,50,stdin);
    input[strcspn(input, "\n")] = 0;
    InLength=strlen(input);
    
    for (int i=0; i < InLength; i++){
        if (input[i] == ' ') {
            output[word][character] = 0; // add 0 to end of previous string
            character = 0;
            word++;
        }
        else {
            output[word][character]=input[i];
            character++;
        }
    }

    output[word][character] = 0; //  add zero to end of last string

    for (word;word>=0;word--){
        printf("%s ",output[word]);
    }
    printf("\n");
}