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