char str[100];
puts("Enter the string: ");
// fgets(str, 100, stdin);
gets(str);
for (int i = 0; i < strlen(str); i++) {
if (isupper(str[i]) > 0) {
str[i] = tolower(str[i]);
}
if (islower(str[i]) > 0) {
str[i] = toupper(str[i]);
}
}
printf("%s", str);
MY TOLOWER FUNCTION DOES NOT WORK FOR EXAMPLE
MY INPUT IS: TURAL muzafarov
but it returns
TURAL MUZAFAROV
OP's code errantly converts to lower and then back to upper
Other issues
Wrong test
is...()
returns zero or non-zero. Testing the sign bit is amiss. @Eric PostpischilUndefined behavior when
str[i] < 0
Avoid that by accessing the string as
unsigned char
Do not use
gets()
It is no longer part of C. Why is the gets function so dangerous that it should not be used?
Avoid repeatedly calculating the string length
Simple test for the null character
Amended code with some other fixes too.