Table of contents
1.
Introduction 
1.1.
Sample Examples
2.
Approach 1 
2.1.
Steps of Algorithm
2.2.
Implementation in C
3.
Approach 2 
3.1.
Steps of Algorithm
3.2.
Implementation in C
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024

Convert uppercase to lowercase

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

We are given a string of characters, in which each character can be lowercase or uppercase. We need to completely convert the given string to lowercase characters. 

Suppose I am given a string “codiNG NINjas,” then we need to convert this string into “coding ninjas.” 

Also Read, Binary to Hex Converter, C Static Function

Sample Examples

Example 1:

Input: “hELlo World”
Output: “hello world”
Explanation: All the characters of the input string is converted to lowercase. 

 

Example 2:

Input: “I love Coding NINJAS”
Output: “i love coding ninjas” 
Explanation: All the characters of the input string is converted to lowercase. 

Approach 1 

We will use the ASCII values concept, ASCII values of upper case characters range from 65 to 90, and lower case ASCII values range from 97 to 112. Forex: the ASCII value of ‘A’ is 65, and the ASCII value of ‘a’ is 97. We can see a gap of 32 between ASCII values of uppercase and lowercase characters. So we check for each character in the string, and if the character is uppercase, i.e., ASCII value is between 65 and 90, we will add 32 into it to make the corresponding lowercase character. In this way, all uppercase characters will be converted to lowercase characters. 

Steps of Algorithm

  1. Store the input in string 
  2. Traverse the string, and check if there is an uppercase character.
  3. If we find the uppercase character, we will add 32 into it to make the corresponding lowercase character.
  4. After complete traversal, our string will be converted to a lowercase character. 

Implementation in C

#include<stdio.h>

#include<ctype.h>

// utility function to convert uppercase to lowercase characters
void convertLowerCase(char arr[100]) {
    int i = 0;
    while (arr[i] != '\0') {
        // if the current character is uppercase
        // finding out by looking at its ascii values
        if (arr[i] >= 65 && arr[i] <= 90) {
            // add 32 to it, to make the corresponding lowercase character
            arr[i] = arr[i] + 32;
        }
        i++;
    }
    return;
}
int main() {
    char arr[100] = "I love Coding NINJAS";
    printf("Original Input: ");
    printf("%s", arr);
    // calling the utility function to convert the string to lowercase
    convertLowerCase(arr);
    printf("\nConverted Input: ");
    printf("%s", arr);

}
You can also try this code with Online C++ Compiler
Run Code

 

Output: 

Original Input: I love Coding NINJAS
Converted Input: i love coding ninjas

You can also read about dynamic array in cShort int in C Programming and  Tribonacci Series

Approach 2 

To convert the given string into the lowercase character string, we can use the inbuilt function ‘tolower’ which will convert the string to lowercase char by char. 

Steps of Algorithm

  • Store the input in the string 
  • Traverse the string character by character and pass that character to the function tolower(), to convert the character to lowercase
  • Print the final string. 

Implementation in C

#include<stdio.h>

#include<ctype.h>

// utility function to covert uppercase to lowercase characters
void convertLowerCase(char arr[]) {
    int i = 0;
    while (arr[i] != '\0') {
        arr[i] = tolower(arr[i]);
        i++;
    }
    return;
}
int main() {
    char arr[100] = "I love Coding NINJAS";
    printf("Original Input: ");
    printf("%s", arr);
    // calling the utility function to convert the string to lowercase
    convertLowerCase(arr);
    printf("\nConverted Input: ");
    printf("%s", arr);
}
You can also try this code with Online C++ Compiler
Run Code

 

Output: 

Original Input: I love Coding NINJAS
Converted Input: i love coding ninjas

You can try an online c editor for good practice.

Frequently Asked Questions

  1. What is the meaning of ASCII? 
    ASCII is the most widely used character encoding scheme for text data on computers and the internet (American Standard Code for Information Interchange). There are unique values for 128 alphabetic, numeric, or special additional characters and control codes in conventional ASCII-encoded data.
     
  2. What is the range of ASCII values of integers?   
    ASCII value range for integers is between 48 to 57. ASCII Value of ‘0’ is 48, and ASCII value of ‘9’ is 57. 
     
  3.  What is the ASCII Value of black space?  
    The ASCII value of blank space is 32. 

Conclusion

In this article, we discussed the problem of converting the given string of uppercase characters into lowercase characters, discussed some sample examples to better understand the problem, and then learned the solution approach along with algorithms and code in c++. We hope you understand the problem and solution properly. Now you can do more similar questions. 

If you are a beginner, interested in Coding, and want to learn DSA, you can look for our guided path for DSA, which is free! 

Thank you for reading. 

Until then, Keep Learning and Keep Coding.

Live masterclass