Convert Lowercase Letters to Uppercase in a Character Array
To convert lowercase letters to uppercase in a character array in C, we iterate through the array and modify each character using the toupper()
function from the ctype.h
library or by subtracting 32 from ASCII values of lowercase letters. This ensures all lowercase letters are converted while non-alphabetic characters remain unchanged.
Examples of Converting Lowercase to Uppercase
1. Convert Lowercase to Uppercase Using toupper()
Function
In this example, we will iterate through a character array (string) and convert each lowercase letter to uppercase using the toupper()
function from ctype.h
.
main.c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "hello world!";
int i = 0;
// Iterate through the character array
while (str[i] != '\0') {
str[i] = toupper(str[i]); // Convert lowercase to uppercase
i++;
}
printf("Uppercase String: %s\n", str);
return 0;
}
Explanation:
- We declare a character array
str[]
initialized with the string"hello world!"
. - We use an integer variable
i
to traverse the array. - Inside a
while
loop, we check if the current character is not the null terminator'\0'
. - We apply the
toupper()
function, which converts lowercase letters to uppercase. - The loop continues until we reach the end of the string.
- The modified string is printed using
printf()
.
Output:
Uppercase String: HELLO WORLD!
2. Convert Lowercase to Uppercase Without toupper()
In this example, we convert lowercase letters to uppercase manually by subtracting 32 from the ASCII value of each lowercase character.
main.c
#include <stdio.h>
int main() {
char str[] = "c programming";
int i = 0;
// Iterate through the character array
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32; // Convert lowercase to uppercase
}
i++;
}
printf("Uppercase String: %s\n", str);
return 0;
}
Explanation:
- We declare a character array
str[]
initialized with"c programming"
. - We use an integer
i
to traverse the array. - Inside a
while
loop, we check if the character is lowercase ('a'
to'z'
). - If it is lowercase, we subtract 32 from its ASCII value to convert it to uppercase.
- The loop continues until we reach the null terminator
'\0'
. - The modified string is printed using
printf()
.
Output:
Uppercase String: C PROGRAMMING
3. Convert Lowercase to Uppercase Using for
Loop
In this example, we will use a for
loop instead of a while
loop to traverse the character array and convert each lowercase letter to uppercase.
main.c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Apple Banana";
int length = sizeof(str) / sizeof(str[0]);
// Convert lowercase letters to uppercase using for loop
for (int i = 0; i < length; i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase String: %s\n", str);
return 0;
}
Explanation:
- We declare a character array
str[]
with the string"example text"
. - We determine the string length using
sizeof(str) / sizeof(str[0])
. - We use a
for
loop to iterate over each character. - Inside the loop, we apply
toupper()
to convert lowercase letters. - The final uppercase string is printed using
printf()
.
Output:
Uppercase String: APPLE BANANA
Conclusion
We have explored different ways to convert lowercase letters to uppercase in a character array:
- Using
toupper()
fromctype.h
. - Manually converting using ASCII subtraction.
- Using a
for
loop instead ofwhile
.