Introduction
In this post, you’ll learn how to convert a string into upper case using C programming. Converting strings to upper case is a common task in text processing, useful in formatting, comparisons, and standardizing inputs.
C Program to Convert String into Upper Case
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s", str);
return 0;
}
Explanation
fgets()
is used to read the input string, including spaces.- The
toupper()
function fromctype.h
is used to convert each character to upper case. - A loop processes each character of the string and updates it in place.
Sample Output
Enter a string: hello world
Uppercase string: HELLO WORLD
Use Cases
- Standardizing user inputs (e.g., usernames or email addresses).
- Case-insensitive string comparisons.
- Preprocessing text for search or filtering.