[C] Convert uppercase to lowercase

A funtion to convert a char to its lowercase equivalent, if it has a lowercase equivalent else return 0, without using the funtion tolower() from ctype.h

#include <stdio.h>
 
// Converting uppercase to lowercase
int tolower(int letter)
{   
    if((letter >= 'A') && (letter <= 'Z'))  // Verify uppercase letter
    {
        letter += 'a' - 'A';  // Convert from uppercase to lowercase
        return letter;
    } else  // It is lowercase
        return 0;
}


int main(void)
{   
    char letter = 0;  // Store a character
 
    printf("Enter an uppercase letter: ");
    scanf("%c", &letter);

    if (tolower(letter) != 0)
        printf("You entered an uppercase %c\n", tolower(letter));
    else
        printf("I want a capital letter, Try using the shift key !\n");
    
    return 0;
}

Well, this from upper to lower, what do you guys think about lower to upper ?

Advertisements

Tagged: , ,

Leave a Reply :

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: