How to Convert a String to Uppercase in C
To convert a string to uppercase in C, we can use functions such as toupper()
from ctype.h
, or manual character iteration. Since C does not have a built-in function for this in the standard library, we often implement our own solutions.
In this tutorial, we will explore multiple ways to convert a string to uppercase in C, with well detailed examples.
Examples to Convert String to Uppercase
1. Using toupper()
from ctype.h
to Convert String to Uppercase
In this example, we will convert a string to uppercase by iterating over each character and using the toupper()
function.
main.c
#include <stdio.h>
#include <ctype.h> // Required for toupper()
void convertToUpper(char str[]) {
int i = 0;
while (str[i] != '\0') {
str[i] = toupper(str[i]); // Convert character to uppercase
i++;
}
}
int main() {
char str[] = "hello, world!";
convertToUpper(str);
printf("Uppercase String: %s\n", str);
return 0;
}
Explanation:
- We include
ctype.h
to use thetoupper()
function. - The
convertToUpper()
function iterates over each character of the string. - The
toupper()
function converts lowercase letters to uppercase. - The loop continues until the null character (
'\0'
) is encountered. - The modified string is printed using
printf()
.
Output:
Uppercase String: HELLO, WORLD!
2. Using ASCII Manipulation to Convert String to Uppercase
In this example, we will manually convert lowercase letters to uppercase by modifying their ASCII values.
main.c
#include <stdio.h>
void convertToUpper(char str[]) {
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') { // Check if lowercase
str[i] = str[i] - 32; // Convert to uppercase
}
i++;
}
}
int main() {
char str[] = "Apple banana cherry";
convertToUpper(str);
printf("Uppercase String: %s\n", str);
return 0;
}
Explanation:
- We iterate through each character of the string.
- If the character is between
'a'
and'z'
, we convert it by subtracting 32 (ASCII difference between uppercase and lowercase letters). - We continue until we reach the null character (
'\0'
). - The updated string is printed using
printf()
.
Output:
Uppercase String: APPLE BANANA CHERRY
3. Using Pointers to Convert String to Uppercase
In this example, we will use pointers to traverse and convert the string to uppercase.
main.c
#include <stdio.h>
#include <ctype.h>
void convertToUpper(char *str) {
while (*str) {
*str = toupper(*str);
str++;
}
}
int main() {
char str[] = "Hello Arjun!";
convertToUpper(str);
printf("Uppercase String: %s\n", str);
return 0;
}
Explanation:
- We use a pointer to traverse the string.
- The
toupper()
function is applied to each character. - The pointer moves to the next character until it reaches the null character.
- The final modified string is printed.
Output:
Uppercase String: HELLO ARJUN!
Conclusion
We explored different ways to convert a string to uppercase in C:
- Using
toupper()
: Simple and effective. - Using ASCII manipulation: Works without external libraries.
- Using pointers: Efficient for traversing and modifying strings.