We can change lowercase letters to uppercase letters by using strupr() string function.
strupr(input_string);
The strupr() function is not part of the C standard library, meaning some compilers may not support it. For cross-platform compatibility, it's advisable to write your own implementation for converting strings to lowercase. This ensures your code functions reliably across different systems and compilers.
Example : using strupr() with user input string
#include <stdio.h>
#include <string.h>
int main(void){
char str[100];
printf("Enter any string");
scanf("%s",str);
printf("Output: %s",strupr(str));
return 0;
}
#include <stdio.h>
#include <string.h>
int main(void){
char str[100];
printf("Enter a string: ");
gets(str); // Can use fgets in newer versions for safety
printf("Uppercase: %s\n", strupr(str));
return 0;
}
Output:
Enter a string: plus2net tutorial
Uppercase: PLUS2NET TUTORIAL
Manual Implementation of strupr()
#include <stdio.h>
void to_upper(char *str) {
while (*str) {
if (*str >= 'a' && *str <= 'z') {
*str = *str - 32; // Convert to uppercase
}
str++;
}
}
int main(void){
char str[] = "hello world";
to_upper(str);
printf("%s\n", str); // Output: HELLO WORLD
return 0;
}
Example: Converting Array of Strings
#include <stdio.h>
#include <string.h>
int main(void){
char str_arr[3][20] = {"apple", "banana", "cherry"};
for (int i = 0; i < 3; i++) {
printf("Before: %s\n", str_arr[i]);
strupr(str_arr[i]);
printf("After: %s\n", str_arr[i]);
}
return 0;
}
Output:
Before: apple
After: APPLE
Before: banana
After: BANANA
Before: cherry
After: CHERRY