strupr() and Portable Uppercase Conversion in C

strupr() converts letters in a string to uppercase on implementations that provide it, but it is not part of the ISO C standard library. Use toupper() for portable C.

Portability: treat strupr() as a compiler-specific extension.

Legacy Syntax Top ↑

strupr(string);  // non-standard extension

Portable Uppercase Function Top ↑

#include <ctype.h>
#include <stdio.h>

void to_uppercase(char text[]) {
    for (size_t i = 0; text[i] != '\0'; i++) {
        text[i] = (char)toupper((unsigned char)text[i]);
    }
}

int main(void) {
    char text[] = "plus2net.com 2026!";

    to_uppercase(text);
    printf("%s\n", text);

    return 0;
}

Output

PLUS2NET.COM 2026!

Uppercase an Array of Strings Top ↑

#include <ctype.h>
#include <stdio.h>

void to_uppercase(char text[]) {
    for (size_t i = 0; text[i] != '\0'; i++) {
        text[i] = (char)toupper((unsigned char)text[i]);
    }
}

int main(void) {
    char words[][20] = {"apple", "banana", "c tutorial"};

    for (size_t i = 0; i < 3; i++) {
        to_uppercase(words[i]);
        printf("%s\n", words[i]);
    }

    return 0;
}

Preserved Examples from the Original Tutorial Top ↑

The following variations restore distinct teaching purposes from the original page while using portable C instead of the non-standard strupr().

Convert User Input to Uppercase

#include <ctype.h>
#include <stdio.h>

void to_uppercase(char text[]) {
    for (size_t i = 0; text[i] != '\0'; i++) {
        text[i] = (char)toupper((unsigned char)text[i]);
    }
}

int main(void) {
    char text[100];

    printf("Enter a string: ");
    if (fgets(text, sizeof text, stdin) == NULL) {
        return 1;
    }

    to_uppercase(text);
    printf("Uppercase: %s", text);

    return 0;
}

Case-Insensitive Comparison Without Modifying the Strings

#include <ctype.h>
#include <stdio.h>

int compare_ignore_case(const char *first, const char *second) {
    while (*first != '\0' && *second != '\0') {
        int a = tolower((unsigned char)*first);
        int b = tolower((unsigned char)*second);

        if (a != b) {
            return a - b;
        }

        first++;
        second++;
    }

    return (unsigned char)*first - (unsigned char)*second;
}

int main(void) {
    const char first[] = "apple";
    const char second[] = "Apple";

    printf("%s\n",
           compare_ignore_case(first, second) == 0
               ? "Strings are equal"
               : "Strings are different");

    return 0;
}




plus2net.com






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