Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

what does my reversed array add random characters in C

I wrote code to reverse an array after in C. I'm using C17. In the code the user is asked to input a word, the word then is put into an array and then reversed. The code works for the exception that it adds some random characters and I couldn't figure out why it does that.

Can you help me with that?

Here is my code

    #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:
");

    for(i=0; (c=getchar()) != '
'; ++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] != ''; ++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);


}
question from:https://stackoverflow.com/questions/65557465/what-does-my-reversed-array-add-random-characters-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function inputtoarray does not input a string. As a result this loop

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

}

within the function reverseinput results in undefined behavior.

The function inputtoarray can look for example the following way

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:
");

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

    input[i] = '';    
}

and called like

inputtoarray( input, MAXLINE );

Moreover this loop

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

does not set the element temp[0] due to the condition coutput > 0. So the first element of the array temp has an indeterminate value.

And this assignment

 input1 = temp;

does not make a sense because it changes the local variable input instead of changing the array pointed to by the pointer (parameter) input.

Without using standard string functions the program can look the following way.

#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:
");

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

    input[i] = '';    
}

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;
}

Its output might look like

Please type in a word:
Hello
olleH

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...