nabeel khan
nabeel khan

Reputation: 5

toggle string using toupper and tolower functions

#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.

Upvotes: 0

Views: 52

Answers (1)

Jarvis__-_-__
Jarvis__-_-__

Reputation: 242

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;
}

Upvotes: 2

Related Questions