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
- Store the input in string
- Traverse the string, and check if there is an uppercase character.
- If we find the uppercase character, we will add 32 into it to make the corresponding lowercase character.
- 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);
}
Output:
Original Input: I love Coding NINJAS
Converted Input: i love coding ninjas
You can also read about dynamic array in c, Short int in C Programming and Tribonacci Series