0
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char str[20],temp;
    printf("enter the string\n");
    gets(str);
    for(int i = 0; i < strlen(str); i++)
    {
        if(tolower(str[i])){
            str[i] = toupper(str[i]);
        }else{
            printf("enterred\n");
            str[i] = tolower(str[i]);
        }
    }
    printf("%s\n", str);
    return 0;
}

I am not able to understand why is this not working the lowercase is turned to uppercase but the wise versa is not happening. it is a very simple program but still, I am not able to understand.

1 Answers1

2

Try this...

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char str[20],temp;
    printf("enter the string\n");
    fgets(str, sizeof(str), stdin);
    for(int i = 0; i < strlen(str); i++)
    {
        if(islower(str[i])){
            str[i] = toupper(str[i]);
        }else{
            printf("enterred\n");
            str[i] = tolower(str[i]);
        }
    }
    printf("%s\n", str);
    return 0;
}
Jarvis__-_-__
  • 287
  • 1
  • 13
  • 1
    Note that `fgets()` includes new line character in the string copied to `str`, if there is any. May you want to check and replace it with null terminating character. – H.S. Jun 16 '21 at 10:27