Convert Uppercase Letters to Lowercase in a Character Array
To convert uppercase letters to lowercase in a character array in C, we iterate through each character and use the built-in tolower()
function from ctype.h
or manually adjust the ASCII values by adding 32
. This ensures that all uppercase characters (A-Z) are converted to their lowercase equivalents (a-z) while leaving other characters unchanged.
Examples of Converting Uppercase Letters to Lowercase
1. Using tolower()
Function to Convert Uppercase to Lowercase
In this example, we will convert uppercase letters to lowercase using the tolower()
function from the ctype.h
library.
main.c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "HELLO WORLD";
int i = 0;
// Convert uppercase letters to lowercase
while (str[i] != '\0') {
str[i] = tolower(str[i]);
i++;
}
printf("Converted String: %s\n", str);
return 0;
}
Explanation:
- We declare a character array
str[]
initialized with “HELLO WORLD”. - We use a
while
loop to iterate through each character until we reach the null terminator'\0'
. - Inside the loop, we use the
tolower()
function to convert uppercase letters to lowercase. - The modified string is then printed using
printf()
.
Output:
Converted String: hello world
2. Using ASCII Value Adjustment to Convert Uppercase to Lowercase
In this example, we will manually convert uppercase letters to lowercase by adding 32
to their ASCII values.
main.c
#include <stdio.h>
int main() {
char str[] = "C PROGRAMMING";
int i = 0;
// Convert uppercase letters to lowercase using ASCII values
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32;
}
i++;
}
printf("Converted String: %s\n", str);
return 0;
}
Explanation:
- We declare a character array
str[]
initialized with “C PROGRAMMING”. - We iterate through each character using a
while
loop until we reach'\0'
. - We check if the character is uppercase using
if (str[i] >= 'A' && str[i] <= 'Z')
. - If true, we convert it to lowercase by adding
32
to its ASCII value. - The modified string is printed using
printf()
.
Output:
Converted String: c programming
3. Handling Mixed Case Strings while Converting Uppercase to Lowercase
In this example, we handle strings that contain a mix of uppercase, lowercase, and special characters while ensuring that only uppercase letters are converted.
main.c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Apple, Banana!";
int i = 0;
// Convert uppercase letters to lowercase while preserving other characters
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = tolower(str[i]);
}
i++;
}
printf("Converted String: %s\n", str);
return 0;
}
Explanation:
- We declare a character array
str[]
initialized with “Hello, C Programming!”. - We use a
while
loop to iterate through each character. - Uppercase characters are converted using
tolower()
, while other characters remain unchanged. - The final result is displayed using
printf()
.
Output:
Converted String: apple, banana!
Conclusion
We explored different methods to convert uppercase letters to lowercase in a character array:
- Using
tolower()
function: The simplest and most efficient method. - Using ASCII value adjustment: Manually converting by adding
32
to the ASCII values. - Handling mixed case strings: Ensuring that only uppercase letters are converted while preserving other characters.