-2

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


}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sp1cyChef
  • 23
  • 5

1 Answers1

3

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

for (cinput=0; input1[cinput] != '\0'; ++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:\n");

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

    input[i] = '\0';    
}

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

Its output might look like

Please type in a word:
Hello
olleH
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • inputtoarray was meant to take the user's input and put it in an array. Do you take into consideration that the code gives me the expected output but with added random characters – Sp1cyChef Jan 04 '21 at 04:01
  • @Sp1cyChef And what? What is unclear with my answer? – Vlad from Moscow Jan 04 '21 at 04:02
  • I don't understand. If all that is wrong with my code how come I'm so close to the wanted final result. I'm trying to learn C here so If you can give me more basic details I would appreciate that – Sp1cyChef Jan 04 '21 at 04:06
  • @Sp1cyChef The code has undefined behavior. The uninitialized array input can contain any garbage but you rely on that it contains a terminating zero character. – Vlad from Moscow Jan 04 '21 at 04:06
  • See [Does "Undefined Behavior" really permit *anything* to happen ...](https://stackoverflow.com/q/32132574/3422102) – David C. Rankin Jan 04 '21 at 04:11
  • @VladfromMoscow I tried to use the inputtoarray you suggested and I'm still getting 'olleh when I type hello. Can you edit my code to make it work so I can see where I went wrong. I'm not familiar with undefined behavior so I will have to read about it more – Sp1cyChef Jan 04 '21 at 04:39