strupr(): Lowercase to Uppercase

#include <stdio.h>
#include <string.h>
int main(void){
   char str[]="plus2net";
   printf("%s\n",strupr (str));
   return 0;
}
The output of above code is here
PLUS2NET
We can change lowercase letters to uppercase letters by using strupr() string function.
strupr(input_string);

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;
}
Output
plus2net
PLUS2NET

Handling Strings with Special Characters

#include <stdio.h>
#include <string.h>

int main(void){
   char str[] = "plus2net.com 2024!";
   printf("%s\n", strupr(str)); 
   return 0;
}
Output:
PLUS2NET.COM 2024!

Example: Converting User Input to Uppercase

#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

Example: Case-Insensitive String Comparison

#include <stdio.h>
#include <string.h>

int case_insensitive_cmp(char *str1, char *str2) {
    return strcmp(strupr(str1), strupr(str2));
}

int main(void){
    char str1[] = "apple";
    char str2[] = "Apple";

    if(case_insensitive_cmp(str1, str2) == 0) {
        printf("Strings are equal\n");
    } else {
        printf("Strings are different\n");
    }

    return 0;
}
Output:
Strings are equal


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer