I am making a simple function to convert a word to all lower case letters. While this function works perfectly for its first instance on line 52, but getting messed up when returning from line 53 on its second instance. Can someone explain me what's going on?
Code:
#include <stdio.h>
#include <stdlib.h>
char* eraseNewLine(char* word)
{
int i=0;
while(word[i]!='\0')
{
if(word[i]=='\n')
{
word[i]='\0';
}
i++;
}
return word;
}
char* toLower(char* word)
{
char* wrd_cpy = word;
int i = 0;
while(word[i]!= '\0')
{
if(word[i]>='A'&&word[i]<='Z')
{
wrd_cpy[i]=(char)((int)word[i]+32);
}
i++;
}
return wrd_cpy;
}
int main()
{
char word_1[20], word_2[20];
char* word_1_cpy, word_2_cpy;
printf("Enter the word_1: ");
fgets(word_1, 20, stdin);
printf("Enter the word_2: ");
fgets(word_2, 20, stdin);
eraseNewLine(word_1);
eraseNewLine(word_2);
word_1_cpy = toLower(word_1);
word_2_cpy = toLower(word_2);
printf("%s and %s", word_1_cpy, word_2_cpy);
return 0;
}