【问题标题】:what does my reversed array add random characters in C我的反向数组在 C 中添加了什么随机字符
【发布时间】:2021-04-09 22:31:19
【问题描述】:

我在 C 中编写了反转数组的代码。 我正在使用 C17。在代码中要求用户输入一个单词,然后将该单词放入一个数组中 然后反转。 该代码适用于它添加一些随机字符的异常,我无法弄清楚它为什么这样做。

你能帮我解决这个问题吗?

这是我的代码

    #include <stdio.h>
#define MAXLINE 80

void inputtoarray(char input[]);                //Take the input from user and put it into an array
void reverseinput(char input1[]);


int main(){

    char input[MAXLINE];
    inputtoarray(input);
    reverseinput(input);


    return 0;
}

void inputtoarray(char input[]){
    int c;                  //to hold the indiviual characters before going into the array
                            //int was used over char because I want to be able to hold EOF
    int i;                  //i is the array counter
    //ask the user to type in a word
    printf("Please type in a word:\n");

    for(i=0; (c=getchar()) != '\n'; ++i){
        input[i] = c;
    }

}

void reverseinput(char input1[]){
    int cinput;
    int coutput;
    char temp[MAXLINE];         //define a temporary array

    //count the number of characters in the array
    for (cinput=0; input1[cinput] != '\0'; ++cinput){

    }
    coutput=cinput;

    //the reversing process. Here cinput holds the number of the last character
    for (cinput=0; coutput > 0; --coutput, ++cinput ){
        temp[coutput] = input1[cinput];
        //input1[coutput] = temp[coutput];
        //printf("%s", temp);
    }
    input1 = temp;
    printf("%s", input1);


}

【问题讨论】:

  • 你没有用\0终止input
  • 请提供输入、预期输出和实际输出,以便我们有一个示例,我们知道您的测试将无法正常工作
  • 看看你的代码;你在哪里为你的输入添加一个 NUL 终止符? (提示:你没有)。传递长度通常比依赖 '\0' 更安全
  • @IrAM 我将如何以 \0 终止
  • @TheGrandJ 例如,如果你运行代码并输入 hello 你会得到 `olleh

标签: arrays c reverse c-strings function-definition


【解决方案1】:

函数inputtoarray 没有输入字符串。结果这个循环

for (cinput=0; input1[cinput] != '\0'; ++cinput){

}

在函数 reverseinput 内会导致未定义的行为。

函数inputtoarray可以看如下方式

void inputtoarray( char input[], size_t n )
{
    int c;                  //to hold the indiviual characters before going into the array
                            //int was used over char because I want to be able to hold EOF
    size_t i = 0;           //i is the array counter
    
    //ask the user to type in a word
    printf("Please type in a word:\n");

    for ( ; i + 1 < n && ( c = getchar() ) != EOF && c != '\n'; ++i )
    {
        input[i] = c;
    }

    input[i] = '\0';    
}

并像这样称呼

inputtoarray( input, MAXLINE );

还有这个循环

for (cinput=0; coutput > 0; --coutput, ++cinput ){
    temp[coutput] = input1[cinput];
    //input1[coutput] = temp[coutput];
    //printf("%s", temp);
}

由于条件coutput &gt; 0,不设置元素temp[0]。所以数组temp的第一个元素有一个不确定的值。

还有这个任务

 input1 = temp;

没有意义,因为它改变了局部变量input,而不是改变了指针(参数)input所指向的数组。

不使用标准字符串函数,程序可以如下所示。

#include <stdio.h>

#define MAXLINE 80

void inputtoarray( char input[], size_t n )
{
    int c;                  //to hold the indiviual characters before going into the array
                            //int was used over char because I want to be able to hold EOF
    size_t i = 0;           //i is the array counter
    
    //ask the user to type in a word
    printf("Please type in a word:\n");

    for ( ; i + 1 < n && ( c = getchar() ) != EOF && c != '\n'; ++i )
    {
        input[i] = c;
    }

    input[i] = '\0';    
}

void reverseinput( char input[] )
{
    size_t n = 0;
    
    while ( input[n] ) ++n;
    
    if ( n != 0 )
    {
        for ( size_t i = 0; i < --n; i++ )
        {
            char c = input[i];
            input[i] = input[n];
            input[n] = c;
        }
    }
}

int main(void) 
{
    char input[MAXLINE];
    
    inputtoarray( input, MAXLINE );
    reverseinput( input );
    
    puts( input );
    
    return 0;
}

它的输出可能看起来像

Please type in a word:
Hello
olleH

【讨论】:

  • inputtoarray 旨在获取用户的输入并将其放入数组中。您是否考虑到代码给了我预期的输出但添加了随机字符
  • @Sp1cyChef 还有什么?我的回答有什么不清楚的地方?
  • 我不明白。如果我的代码所有这些都是错误的,我怎么会如此接近想要的最终结果。我正在尝试在这里学习 C,所以如果你能给我更多的基本细节,我将不胜感激
  • @Sp1cyChef 代码具有未定义的行为。未初始化的数组输入可以包含任何垃圾,但您依赖它包含终止零字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 2021-06-10
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多