I was doing some tests with an online judge, my code is as follows:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i;
char mystring[100];
scanf("%s",mystring);
char newString[strlen(mystring)];
for(i=0;i<strlen(mystring);i++){
if(mystring[i]==toupper(mystring[i])){
newString[i] = tolower(mystring[i]);
}else{
newString[i] = toupper(mystring[i]);
}
}
printf("%s",newString);
}
The code changes the uppercase characters to lowercase and the lowercase characters to uppercase, the problem is that when compiling it throws me that it adds a strange letter at the end of the string:
Entry:
RrHRxcUsSrvSnTyolvsxHoGyzwBMzuLCUjcSeWmBAhodtEkgZjDkFjaXXAvjTvRfHuHGtopoxaeONzFFurfNlRdAvRYlnlfdIMsI
Expected output:
rRhrXCuSsRVsNtYOLVSXhOgYZWbmZUlcuJCsEwMbaHODTeKGzJdKfJAxxaVJtVrFhUhgTOPOXAEonZffURFnLrDaVryLNLFDimSi
My output:
rRhrXCuSsRVsNtYOLVSXhOgYZWbmZUlcuJCsEwMbaHODTeKGzJdKfJAxxaVJtVrFhUhgTOPOXAEonZffURFnLrDaVryLNLFDimSiÿ
As you can see it does its job perfectly except that it produces an "ÿ", where does this come from?
When compiling it from my own PC I don't see this symbol but this may be because it isn't in the alphabet on my PC or the IDE just doesn't support it.
I can fix this by simply removing the character from the array as it always happens but still this shouldn't happen.