C++ program to convert the lower case to uppercase and vice versa of the given string

View Discussion

Show

Improve Article

Save Article

Like Article

Given a string, write a function that converts it either from lower to upper case or from upper to lower case using the bitwise operators &(AND), |(OR), ~(NOT) in place and returns the string. Many of us know that Bitwise manipulations are faster than performing arithmetic operations for a compiler as the data is stored in binary form 0’s and 1’s. Examples:

Input : "LowerToUpPer" Output : "LOWERTOUPPER" Letters already in the uppercase remains the same. while rest get converted to uppercase. Input : "UPPerTOloweR" Output : "uppertolower" Letters already in the lowercase remains the same. while rest get converted to lowercase.

1.Lower to Upper Case This method simply subtracts a value of 32 from the ASCII value of lowercase letter by Bitwise ANDing (&) with negation (~) of 32 converting the letter to uppercase. 

Implementation:

#include<stdio.h>

const int x = 32;

char *toUpperCase(char *a)

{

    for (int i=0; a[i]!='\0'; i++)

        a[i] = a[i] & ~x;

    return a;

}

int main()

{

    char str[] = "SanjaYKannA";

    printf("%s", toUpperCase(str));

    return 0;

}

Time complexity : O(n) 
Auxiliary Space : O(1

2.Upper to Lower Case Similarly, it adds a value of 32 to the ASCII value of uppercase letter by Bitwise ORing (|) with 32 converting the letter to lowercase. 

Implementation:

#include<stdio.h>

const int x = 32;

char * toLowerCase(char *a)

{

    for (int i=0; a[i]!='\0'; i++)

        a[i] = a[i] | x;

    return a;

}

int main()

{

    char str[] = "SanjaYKannA";

    printf("%s", toLowerCase(str));

    return 0;

}

Time complexity : O(n) 
Auxiliary Space : O(1) 

Explanation: The ASCII table is constructed in such way that the binary representation of lowercase letters is almost identical of binary representation of uppercase letters. Character ‘A’ is integer 65 = (0100 0001)2, while character ‘a’ is integer 97 = (0110 0001)2. The difference between the ASCII values of ‘a’ and ‘A’ is 32. So we can easily change the case of the letters either from Upper to lower or lower to upper by adding or subtracting the difference from the letters using bitwise operators as shown above. 

Exercise: Implement a function that change the case of a string such that GeeksFoRgeekS turns gEEKSfOrGEEKs .   

This article is contributed by Sanjay Kumar Ulsha from JNTUH College Of Engineering, Hyderabad. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.


Write a C Program to Convert lowercase to uppercase and vice versa. Here’s simple C Program to Convert lowercase to uppercase and vice versa in C Programming Language.

C – Strings : 

Strings are actually one-dimensional array of characters terminated by a null character ‘\0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.

String is a sequence of characters. char data type is used to represent one single character in C. So if you want to use a string in your program then you can use an array of characters.

The declaration and definition of the string using an array of chars is similar to declaration and definition of an array of any other data type.

Any string ends with a terminating null character ‘\0’. An array definition in such a way should include null character ‘\0’ as the last element.

Here is source code of the C Program to Convert lowercase to uppercase and vice versa. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

SOURCE CODE : :

/* C Program to Convert lowercase to uppercase and vice versa */ #include<stdio.h> #include<string.h> int main() { char str[20]; int i; printf("\nEnter any string :: "); gets(str); printf("\nThe input String is :: [ %s ]\n",str); for(i=0;i<=strlen(str);i++) { if(str[i]>=97&&str[i]<=122) str[i]=str[i]-32; else if(str[i]>=65&&str[i]<=90) str[i]=str[i]+32; else; } printf("\nThe Converted String is :: [ %s ]\n",str); return 0; }

OUTPUT : :

/* C Program to Convert lowercase to uppercase and vice versa */ Enter any string :: CodezClub The input String is :: [ CodezClub ] The Converted String is :: [ cODEZcLUB ] Process returned 0

Above is the source code for C Program to Convert lowercase to uppercase and vice versa which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

Thanks for reading the post….

In this tutorial, we will see how to convert uppercase to lowercase in C. It is very easy to convert uppercase to lowercase in c.

Let’s say if you have any character in uppercase, you can simply convert it by just adding 32 to its ascii value.

In similar way, you can substract 32 from ascii character to convert lowercase to uppercase.

Let’s write complete program to convert uppercase to lowercase and vice versa.

    printf("\n\nEnter The String: ");

    // Calculating length of input string

        if (str[i]>=65 && str[i]<=90)

        else if (str[i] >= 97 && str[i] <= 122)

    printf("\nConverted String(Lower/Upper) Is: %s\n",str);

Output:

Enter The String: code2master

Converted String(Lower/Upper) Is: CODE2MASTER

Enter The String: CODE2MASTER

Converted String(Lower/Upper) Is: code2master

That’s all about convert uppercase to lowercase and vice versa.