How to Change Case of Alphabets in a String in C

How to Change Case of Alphabets in a String C Language

In this article, I will discuss How to Change the Case of Alphabets in a String C Language with Examples. Please read our previous article discussing How to Find the Length of a String in C Language with Examples.

How to Change the Case of Alphabets in a String in C Language?

Now, we will see how to change the case of alphabets from lowercase to uppercase or vice versa of characters in a string. Please have a look at the following string:

How to Change Case of Alphabets in a String in C Language?

Here, we have taken the string “hello”. So, all letters are in lowercase. We have to change it to uppercase. We have seen the ASCII code for alphabets:

Uppercase Alphabets: 65 (A) to 90 (Z)

Lowercase Alphabets: 97 (a) to 122 (z)

We know these cases, so it means when it is a small ‘h’, I have to change it to an uppercase ‘H’. So,

  1. for the small ‘h’ code is 104, and for the capital ‘H’ code is 72, the difference is 32.
  2. for small ‘e’ code is 101, and for capital ‘E’ code is 69, the difference is 32.
  3. for the small ‘l’ code is 108, and for the capital ‘L’ code is 76, the difference is 32.
  4. for the small ‘o’ code is 111, and for the capital ‘L’ code is 79, the difference is 32.

So, the difference is 32 for every small and capital alphabet. So, it means that the uppercase and lowercase letters differences are 32. If any letter is in lowercase and we subtract 32 from that, we will get an uppercase ASCII code.

So, it is possible to add a number to the alphabet. Actually, the alphabet or letters are nothing but codes. For a programmer or the user, it is visible as ‘h,’ but actually, it stores the ASCII code inside the main memory. So, we can modify it by adding some numbers.

So, how do we convert from lowercase to uppercase, which means subtract 32? Let us give it a try.  Let us write the program for converting the cases, and we have to scan through all these alphabets. For that, we need a ‘for’ loop:

Lowercase to Uppercase Code in C Language:
#include <stdio.h>
#include <stdlib.h>
int main(){
     char B[] = "hello";
     int i;
     for(i = 0; B[i] != '\0'; i++){
          if(B[i] >= 'a' && B[i] <= 'z'){
             B[i] -= 32;
         }
    }
    printf("%s", B);
}
Output:

Lowercase to Uppercase Code in C Language

Uppercase to Lowercase Code in C Language:
#include <stdio.h>
#include <stdlib.h>
int main(){
     char B[] = "HELLO";
     int i;
     for(i = 0; B[i] != '\0'; i++){
          if(B[i] >= 'A' && B[i] <= 'Z'){
             B[i] += 32;
         }
    }
    printf("%s", B);
}
Output:

Uppercase to Lowercase Code in C Language

strupr() and strlwr() Built-in Function in C

We can also use the strupr() and strlwr() Built-in Function in C to convert the string to lowercase and uppercase, respectively. The strupr() and strlwr() are not standard C functions (they’re not defined in ANSI/ISO C and not part of the standard C library); they are sometimes found in specific compilers/platforms. For a better understanding, please have a look at the following example. If your compiler does not support it, then you will get a compiler time error.

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

int main() {
    char str[] = "Hello, World!";
    strupr(str);
    printf("Uppercase: %s\n", str);
    strlwr(str);
    printf("Lowercase: %s\n", str);
    return 0;
}
How to Toggle the cases of a string in C language

We have already seen how to scan through a string or how to traverse a string using a ‘for’ loop. The next thing I will show you is how to toggle the cases. If it is lowercase, then convert it to uppercase and vice versa. Let us look at how to toggle the cases of alphabets in a string.

char S = “HeLlo”;

I have the alphabet above; some are in uppercase, like ‘H’ and ‘L’, and the rest are lowercase. So, we will change whichever are in lowercase into an uppercase and uppercase to lowercase. So, if it is in uppercase, we have to add 32; if it is lowercase, we have to subtract 32. For that, we have to scan for this whole string.

Toggle the cases of a string Code in C language:
#include <stdio.h>
#include <stdlib.h>
int main(){
     char B[] = "HeLlO";
     int i;
     for(i = 0; B[i] != '\0'; i++){
         if(B[i] >= 65 && B[i] <= 90)
              B[i] += 32;
         else if(B[i] >= 'a' && B[i] <= 'z'){
             B[i] -= 32;
         }
    }
    printf("%s", B);
}
Output:

How to Toggle the cases of a string in C language

In the next article, I will discuss How to Count Vowels and Consonants in a String in C Language with Examples. In this article, I try to explain How to Change the Case of the Alphabet in a String in C Language with Examples. I hope you enjoy this article, “How to Change Case of Alphabets in a String in C Language with Examples”. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *