Convert String from Lower case to Upper case in C Language

Convert-String-from-Lower-case-to-Upper-case-in-C-Language

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.

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

  1. Declare a string with the desired size. Let’s say we created a string named inputStr.
  2. 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.
  3. 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.
  4. 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.
  5. Once the above loop is completed, All the Lower case characters in the given string will be converted into Upper Case characters.
  6. 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.

Program Output:

Let’s compile and Run the program.

convert-lower-case-to-upper-case-using-iterative-method-program-output

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.

Program Output:

Compile and Run the program.

convert-lower-case-to-upper-case-using-user-defined-functions-program

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.

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

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.

Program Output:

Let’s compile and run the program using the GCC compiler.

convert-lower-case-to-upper-case-using-library-functions-in-c

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.

If your implementation supports you can use the struprfunction like below.

📢 If strupr() function doesn’t work try the _strupr() function

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Convert Lower case string to Upper case string […]

Leave a Reply