Convert String from Uppercase to Lowercase in C Language

Program-to-Convert-String-from-Uppercase-to-Lowercase-in-C-Language

Program Description:

Write a Program to convert a string from uppercase to lowercase in c programming language. The program will accept a string from the user and converts all upper-case characters in the given string to lower-case characters. If the string contains lowercase characters, then we should not change them.

📢 This Program is part of the C String Practice Programs series

Here are the program input and output

Example Input and Output:

Input:

Enter a string : PROGRAMMING IS FUN

Output:

Converted String: programming is fun

Program Prerequisites:

It is good to know the basics of the C-Strings, C-Arrays, and C-Functions. Please read the following articles to learn more about these topics.

In this article, we are going to look at a few methods to convert the string from Lower case to Upper case in C language. Before looking at the program. Let’s understand the logic to convert the string from Uppercase to Lowercase.

Algorithm to Convert String From Uppercase to Lowercase 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

So, To convert any Uppercase character to a Lowercase character, We can Add the number 32 to the ASCII number of the uppercase character.

For example,

  • Uppercase ‘C’ – ASCII Value – 67.
  • To convert Uppercase to a Lowercase character – Add 32 to the Uppercase character. i.e 67 + 32 = 99
  • The ASCII value 99 is equal to the Lowercase character ‘c’

Here is the Algorithm of the program

  1. Declare a string named inputStr with size SIZE( i.e SIZE is a global constant)
  2. Prompt the user to input a string and store it in 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 an Uppercase character, Then Add the number 32 to the ASCII value of the Uppercase character to make it a Lowercase character.
  4. Create a For Loop, Start from i=0 and Go till the terminating NULLcharacter. ( for(i = 0; inputStr[i]; i++) )
    • At each iteration, Check if the inputStr[i] is an Uppercase character using if(inputStr[i] >= ‘A’ && inputStr[i] <= ‘Z’) condition. We can also use is isupper() function from ctype.h header file.
    • If the inputStr[i] is an uppercase character, Then Add the number 32 to the ASCII value of character – inputStr[i] = inputStr[i] + 32;
    • Repeat the above steps until we reach the terminating NULL Character ( \0) of the inputStr.
  5. Once the above loop is completed, All the Uppercase characters in the given string will be converted into LowerCase characters.
  6. Print the results on the console using the printf function.

Method 1: Convert the String from Uppercase to Lowercase in C without using string functions:

Here is the string conversion program.

Program Output:

Let’s compile and Run the program using GCC compiler(Any of your favorite compilers)

convert-string-from-uppercase-to-lowercase-in-c-program-output

The program is properly converting the upper case character to lower case characters.

Method 2: UpperCase string to LowerCase string using user-defined functions in C:

We are going to define two user-defined functions(except the main() function). They are isUpperCaseChar and toLowerCaseChar.

The isUpperCaseChar() function takes a character as input and checks if it is an uppercase character. This function returns One(1) if the given character is upper case, Otherwise it returns Zero(0). This function is similar to isupper() library function.

Similarly, The toLowerCaseChar() function takes an Uppercase character as input and Converts the given character into a Lowercase character and returns the converted string’s ASCII Code. This function is similar to the tolower() string library function.

Here is the program to convert an uppercase string to a lowercase string.

Program Output:

Compile and Run the program.

convert-string-from-uppercase-to-lowercase-in-c-using-user-defined-functions

Method 3: Convert Uppercase to Lowercase string using tolower() function in C:

In this method, We are going to use the two library functions isupper() and tolower() to convert an uppercase string to a lowercase string in C programming language.

The isupper() and tolower() functions are available as part of the ctype.h header file.

Here is the syntax of the isupper() library function.

The isupper() function checks if the character c is an Uppercase character. It returns a Non-Zero value if c is an Uppercase character, Otherwise, It returns Zero.

Syntax of tolower() function

The tolower() function converts the given character c to lowercase characters and returns, its ASCII value.

Here is the program to convert string from uppercase to lowercase characters using C Standard library functions.

Program Output:

Compile and Run the program.

convert-string-from-uppercase-to-lowercase-using-library-functions

Method 4: Convert uppercase string to lowercase using strlwr function:

There is another function called strlwrto convert the string to a lower case string. But strlwrfunction 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)

If your implementation supports you can use the strlwr function like below.

If strlwr function doesn’t work, Then try the _strlwr() 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...

2 Responses

  1. […] we already discussed earlier ( Lowercase to Uppercase conversion, Uppercase to lowercase character conversion programs), characters in C language are stored in the memory with their ASCII […]

  2. […] C Program to Convert Uppercase string to Lowercase string […]

Leave a Reply