如何摆脱这个程序中的垃圾字符?

时间:2015-03-30 05:06:20

标签: c arrays string char user-input

该程序应该向后打印输入字符串。但是,每次发生这种情况时,我都会收到诸如\340之类的垃圾字符。它为什么这样做?这是我的代码:

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

int main()
{
    char mattsentence[51];
    mattsentence[50] = '\0';
    gets(mattsentence);
    char mask[sizeof(mattsentence)];
    int i, j;
    j = sizeof(mattsentence);
    for (i = 0; i < sizeof(mask); i++)
    {
        j = j - 1;
        mask[i] = mattsentence[j];
        printf("%c", mask[i]);
    }
    printf("\n");
}

3 个答案:

答案 0 :(得分:2)

sizeof()运算符给出了数据类型的大小。因此,sizeof(mattsentence)会为您提供51的值。然后,sizeof(mask)会再次为您51

当你使用sizeof(mask)作为for循环条件时,你基本上会超过实际的输入值,从而剔除垃圾值。

这里你想要的是使用strlen()找出输入字符串的实际有效长度。

所以,基本上你需要照顾

第1点:sizeof替换为strlen()

第2点gets()的使用很危险。请使用fgets()代替gets()

第3点: int main()应为int main(void)。在return的末尾添加一个expilicit main()语句。良好做法。

修改后的代码应该是

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

int main(void)
{
    char mattsentence[51] = {0};                  //always initalize local variables, here it's covering null termination , too.

    fgets(mattsentence, 51, stdin);              //fgets()

    char mask[strlen(mattsentence) + 1];         // one more to store terminating '\0'

    int i = 0, j = 0, k = 0;
    j = strlen(mattsentence);
    k = j;
    for (i = 0; i < k; i++)  // make use of k, don't call `strlen()` repeatedly
    {
        j = j - 1;
        mask[i] = mattsentence[j];
        printf("%c", mask[i]);
    }
    mask[i] = '\0'; // for proper string termination
    printf("\n");
    printf("%s\n", mask);

    return 0;                     //added return statement
}

答案 1 :(得分:2)

您的方法是错误的,因为您可以反转整个字符数组,而只能部分填充它。您应该使用标头strlen中声明的标准C函数<string.h>来确定输入字符串的大小。使用gets也是不安全的,因为你可以覆盖字符数组之外的内存。它现在被排除在C标准之外

这里显示了如何编写程序。

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

#define N   51

int main(void) 
{
    char mattsentence[N] = { '\0' };
    char mask[N] = { '\0' };

    fgets( mattsentence, sizeof( mattsentence ), stdin );

    size_t n = strlen( mattsentence );

    if ( n != 0 && mattsentence[n-1] == '\n' ) mattsentence[--n] = '\0';

    for ( size_t i = 0; n != 0; i++ )
    {
        mask[i] = mattsentence[--n];        
        printf( "%c", mask[i] );
    }

    printf( "\n" );

    return 0;
}

如果要输入

Hello, Christiana S. F. Chamon

然后程序输出

nomahC .F .S anaitsirhC ,olleH

考虑到要以相反的顺序输出字符串,不需要定义第二个字符数组。

如果您只想以相反的顺序输出源字符串,那么程序可能看起来像

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

#define N   51

int main(void) 
{
    char mattsentence[N] = { '\0' };

    fgets( mattsentence, sizeof( mattsentence ), stdin );

    size_t n = strlen( mattsentence );

    if ( n != 0 && mattsentence[n-1] == '\n' ) mattsentence[n-1] = '\0';

    while ( n != 0 )
    {
        printf( "%c", mattsentence[--n] );
    }

    printf( "\n" );

    return 0;
}

答案 2 :(得分:0)

查看更改的代码:

int main()
{
    char mattsentence[51];
    mattsentence[0] = '\0'; // initialization

    gets(mattsentence);

    char mask[strlen(mattsentence) + 1]; // +1 for string terminator '\0'

    int i, j;
    j = strlen(mattsentence);
    for (i = 0; i < strlen(mattsentence); i++)  // strlen of original string
    {
        j = j - 1;
        mask[i] = mattsentence[j];
        printf("%c", mask[i]);
    }
    mask[i] = '\0'; // for proper string termination
    printf("\n");
    printf("%s\n", mask);
}

有几个错误:

  • strlen()应该用于获取字符串的长度
  • for循环应根据输入字符串控制,而不是输出字符串
  • 最好使用fgets()代替gets():这样您就可以控制从输入中读取的字符数
相关问题