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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…