OP's code errantly converts to lower and then back to upper
if (isupper(str[i]) > 0) {
str[i] = tolower(str[i]);
}
// All alpha are lower case at this point
if (islower(str[i]) > 0) {
str[i] = toupper(str[i]);
}
Other issues
Wrong test
is...()
returns zero or non-zero. Testing the sign bit is amiss. @Eric Postpischil
// if (isupper(str[i]) > 0) {
if (isupper(str[i])) {
Undefined 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
// for (int i = 0; i < strlen(str); i++) {
for (int i = 0; str[i]; i++) {
Amended code with some other fixes too.
char str[100];
puts("Enter the string: ");
if (fgets(str, sizeof str, stdin)) { // Test fgets result.
unsigned char *ustr = (unsigned char *) str;
for (size_t i = 0; ustr[i]; i++) { // Use size_t to handle even _long_ strings.
if (isupper(ustr[i])) {
ustr[i] = tolower(ustr[i]);
} else if (islower(ustr[i])) { // Notice the `else`
ustr[i] = toupper(ustr[i]);
}
}
}
printf("%s", str);