Convert String from Lower case to Upper case in C Language
- Algorithm to Convert String From Lower case to Upper case in C:
- Method 1: Convert Lower case string to Upper case in C without using string functions:
- Method 2: Convert Lower case string to Upper case string using user-defined functions in C:
- Method 3: Convert Lower case to Upper case using toupper() function in C:
- Method 4: Convert Lowercase string to Uppercase using strupr function in C Programming:
Program Description:
Write a Program to Convert a string from Lower case to Upper case in C Programming Language. The program should take an input string from the user and convert all Lower case characters to upper case characters. If the input string contains uppercase characters, leave them untouched.
📢 This Program is part of the C String Practice Programs series
Here are the input and output of the program
Expected Input and Output:
Input:
Enter a string : learn programming at sillycodes.com
Output:
Converted String: LEARN PROGRAMMING AT SILLYCODES.COM
Prerequisites:
It is recommended to go through the following C-String foundation articles to understand this program better.
- Strings in C Language – Declare, Initialize, and Modify Strings
- Different ways to Read and Print strings in C
- Arrays in C with Example Programs
- Functions in C – How to Create and Use functions in C
We are going to look at the Four methods to convert the string from Lower case to Upper case in C language.
Algorithm to Convert String From Lower case to Upper case in C:
All the characters in the C Language are stored in the memory as an ASCII numbers.
For example, The character upper case A‘s ASCII Value is 65. Similarly lower case a‘s ASCII Value is 97. So the C language store these characters based on their ASCII values.
You can check the list of ASCII Values by looking at the following post
Now we know how C Language stores the ASCII Characters, To convert any Lowercase character to Upper Case character, We can subtract the value 32 from the ASCII number of the lowercase character.
For example,
- Lowercase ‘b’ – ASCII Value – 98.
- To convert to Upper case character – Subtract 32 from the lower case character. i.e 98 – 32 = 66
- The ASCII value of the Upper case ‘B’ is 66.
Here is the Algorithm of the program
- Declare a string with the desired size. Let’s say we created a string named inputStr.
- Take the input string from the user and update the inputStr string. We are using the gets function. use it with caution as it won’t check boundaries.
- Now Iterate over the string character by character and if the character is a lowercase character, Then subtract the number 32 from the ASCII value of the lowercase character to make it an uppercase character.
- Create a For Loop start from
i=0 go till the terminating NULL character. ( for(i = 0; inputStr[i]; i++) )
- At each iteration, Check if the inputStr[i] is lowercase using if(inputStr[i] >= ‘a’ && inputStr[i] <= ‘z’) condition. We can also use is islower() function from ctype.h header file.
- If the inputStr[i] is lowercase, Reduce the ASCII value of it by 32. – inputStr[i] = inputStr[i] – 32;
- Repeat the above steps until we reach the terminating NULL Character.
- Once the above loop is completed, All the Lower case characters in the given string will be converted into Upper Case characters.
- Print the results on the console using the printf function.
Method 1: Convert Lower case string to Upper case in C without using string functions:
Let’s convert the above algorithm into the C code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/*     program to convert lower case string to upper case string     sillycodes.com */  #include<stdio.h>  // Max Size of string const int SIZE = 100;  int main() {      // declare a string with 'SIZE'     char inputStr[SIZE];     int i;      // get the string from the user     printf("Enter a string : ");     gets(inputStr);       // Iterate over the string     for(i = 0; inputStr[i]; i++)     {          // check if the character is a lower case alphabet         if(inputStr[i] >= 'a' && inputStr[i] <= 'z')         {             // Ascii value of 'a' = 97             // Ascii value of 'A' = 65             // To covert lower to upper, Reduce -32 from lower case alphabet value.             // Learn More - https://sillycodes.com/c-program-to-print-all-ascii-characters/             inputStr[i] = inputStr[i] - 32;         }     }      // print the resultant string     printf("Converted String: %s\n", inputStr);      return 0; } |
Program Output:
Let’s compile and Run the program.
1 2 3 4 5 |
$ gcc lower-to-upper.c  $ ./a.out Enter a string : c programming Converted String: C PROGRAMMING $ |
As we can see from the above output, the Program is properly converting the lower-case character to upper-case characters.
Method 2: Convert Lower case string to Upper case string using user-defined functions in C:
Let’s look at the second method to convert the lower case string to an upper case string using user-defined functions.
We are going to define two user-defined functions(except the main() function). They are isLowerCaseChar and toUpperCaseChar.
The isLowerCaseChar() function takes a character as input and checks if it is a lowercase character. This function returns One(1) if the given character is lower case, Otherwise it returns Zero(0). This function is a custom implementation of the islower() library function.
Similarly, The toUpperCaseChar() function takes a lowercase character as input and Converts the given character into an Uppercase character. This function returns the converted string’s ASCII Code. This function is a custom implementation of the toupper() string library function.
Here is the program to convert lowercase string to uppercase string using custom functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/*     program to convert lower case string to upper case string using user defined functions     sillycodes.com */  #include<stdio.h>  // Max Size of string const int SIZE = 100;  /** * @brief  - Check if the 'ch' is lower case character * * @param ch  - input character * @return int - 1 if 'ch' is lower case. Otherwise, Zero(0). */ int isLowerCaseChar(char ch) {     // check if the character is a lower case alphabet     if(ch >= 'a' && ch <= 'z')     {         // ch is lower case character         return 1;     }      return 0; }  /** * @brief  - Convert lower case 'ch' to Upper case character and return it * * @param ch  - input character * @return int - upper case character ascii value */ int toUpperCaseChar(char ch) {     // Ascii value of 'a' = 97     // Ascii value of 'A' = 65     // To covert lower to upper, Reduce -32 from lower case alphabet value.     // Learn More - https://sillycodes.com/c-program-to-print-all-ascii-characters/     return ch - 32; }  int main() {      // declare a string with 'SIZE'     char inputStr[SIZE];     int i;      // get the string from the user     printf("Enter a string : ");     gets(inputStr);       // Iterate over the string     for(i = 0; inputStr[i]; i++)     {          // Call 'isLowerCaseChar' to check if the character is a lower case alphabet         if(isLowerCaseChar(inputStr[i]))         {             // call 'toUpperCaseChar' function to convert the string             inputStr[i] = toUpperCaseChar(inputStr[i]);         }      }      // print the resultant string     printf("Resultant String: %s\n", inputStr);      return 0; } |
Program Output:
Compile and Run the program.
1 2 3 4 5 |
$ gcc lower-to-upper-fun.c $ ./a.out Enter a string : Australia is Wider than the Moon Resultant String: AUSTRALIA IS WIDER THAN THE MOON $ |
Method 3: Convert Lower case to Upper case using toupper() function in C:
In this method, We are going to use the library functions islower() and toupper() to convert a lower case string to an upper case string in C programming language.
The islower() and toupper() functions are available as part of the ctype.h header file.
Here is the syntax of the islower() library function.
1 |
int islower(int c); |
The islower() function checks if the character c is a Lowercase character. It returns a Non-Zero value if c is a lowercase character, Otherwise, It returns Zero.
Syntax of toupper() function
1 |
int toupper(int c); |
The toupper() function converts the given character c to upper case character and returns, it’s ASCII value.
Here is the program to convert lowercase string to uppercase string using library functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/*     program to convert lower case string to upper case string using library functions     sillycodes.com */  #include<stdio.h> #include<ctype.h>  // Max Size of string const int SIZE = 100;  int main() {      // declare a string with 'SIZE'     char inputStr[SIZE];     int i;      // get the string from the user     printf("Enter a string : ");     gets(inputStr);       // Iterate over the string     for(i = 0; inputStr[i]; i++)     {          // use 'islower()' library function to check if the 'ch' is lower case         if(islower(inputStr[i]))         {             // call the 'toupper' library function.             inputStr[i] = toupper(inputStr[i]);         }     }      // print the resultant string     printf("Converted String: %s\n", inputStr);      return 0; } |
Program Output:
Let’s compile and run the program using the GCC compiler.
1 2 3 4 5 |
$ gcc lower-to-upper-library-func.c $ ./a.out Enter a string : Baby rabbits are called kits Converted String: BABY RABBITS ARE CALLED KITS $ |
We are getting the excepted results.
Method 4: Convert Lowercase string to Uppercase using strupr function in C Programming:
There is another function called struprto convert the Lower-case string to the Upper case string. But strupr function is not a C standard function. It is a non-standard function from Microsoft’s C-Lang library. So it might not work on all implementations (compilers/environments)
Here is the syntax of the strupr function.
1 |
char *strupr(char *string); |
If your implementation supports you can use the struprfunction like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/*     program to convert lower case string to upper case string     sillycodes.com */  #include<stdio.h> #include<string.h>  // Max Size of string const int SIZE = 1000;  int main() {      // declare a string with 'SIZE'     char inputStr[SIZE];     int i;      // get the string from the user     printf("Enter a string : ");     gets(inputStr);      // call 'strupr' to conver the string to 'upper case'     printf("Converted String: %s\n", strupr(inputStr));      return 0; } |
📢 If strupr() function doesn’t work try the _strupr() function
1 Response
[…] C Program to Convert Lower case string to Upper case string […]