Here's my code for a function that change the case of the character. For example"ABC" would be turn to "abc" and vice versa.
char *flipCase(const char *src){
char *output;
output = malloc(sizeof(src));
//Copy source to output
strcpy(output,src);
int i = 0;
//Check if the case is lower or upper
while(output[i] != '\0')
{
//Check if it's alphabetic
if (isalpha(output[i]))
{
//if it's upper case
if (isupper(output[i]))
{
//Convert to lower case and increment i
output[i]= tolower(output[i]);
i++;
}
//if it's lower case
if (islower(output[i]))
{
//Convert to upper and increment i
output[i]=toupper(output[i]);
i++;
}
}
//Else, skip it
else
{
i++;
}
}
return output;}
For most of the time, it seems to be fine to me. However when it is tested it with "Hello World, How are you?". I expected "hELLO wORLD, hOW ARE YOU?" but my program gives "hELLO wORLD, hOW ARE YOU1" Notice the "1" at the very end instead of "?". What's causing the problem? How can I fix it?