Funny characters being printed
Hi all! What i'm trying to do is to store the longest string being input into the program and print it out later. However when i print the longest string, it has some wierd characters at the back. Try inputing the string: there
Code:
#define MAX 50
int main()
{
char input[MAX];
char temp [MAX];
char store[MAX]; //longest string stored here
int length=0, i, option, choice;
int maxLength = strlen(store);
choice = 1
switch(choice)
{
case 1: printf("Input = ");
fgets(input, MAX, stdin);
length = stringLength(input);
//temp is needed to presevere what is stored in input
for(i=0;i<=length;i++)
{
temp[i] = input[i];
}
//convertString will manipulate the input
if(convertString(input)==1)
{
if(length>=maxLength)
{
maxLength = length;
for(i=0;i<length;i++)
store[i] = temp[i];
option = 1;
}
}
fputs(store, stdout); //print out the string
break;
//the rest of case n default case goes here
}
return 0;
}
// find length of string in array
int stringLength(char str[])
{
int i = 0;
for(i=0; i<MAX; i++)
{
if(str[i]=='\n')
return i;
}
return i;
}
Please pardon my silly mistakes I've made if any as I am quite new to C.