Check if a String is Uppercase in C

To check if a string is entirely in uppercase in C, you can examine each character using functions like isupper() or by converting the string to uppercase and comparing it with the original. This tutorial demonstrates two approaches to solve the problem.


Example 1: Using isupper() from <ctype.h>

In this example, we create a function that iterates through each character of the string and checks if it is uppercase using the isupper() function. We test the function with two strings to see whether they are completely uppercase.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

// Function to check if the string is completely uppercase
bool isStringUppercase(const char *str) {
    int i = 0;
    while (str[i] != '\0') {
        // Check only alphabetic characters
        if (isalpha(str[i]) && !isupper(str[i])) {
            return false;
        }
        i++;
    }
    return true;
}

int main() {
    char str1[] = "HELLO WORLD";
    char str2[] = "Hello World";

    if (isStringUppercase(str1)) {
        printf("'%s' is uppercase.\n", str1);
    } else {
        printf("'%s' is not uppercase.\n", str1);
    }

    if (isStringUppercase(str2)) {
        printf("'%s' is uppercase.\n", str2);
    } else {
        printf("'%s' is not uppercase.\n", str2);
    }

    return 0;
}

Output:

'HELLO WORLD' is uppercase.
'Hello World' is not uppercase.

Explanation:

  1. The function isStringUppercase iterates over each character of the input string str using a while loop.
  2. It uses isalpha() to check if a character is a letter, and isupper() to verify if that letter is in uppercase.
  3. If any alphabetic character is not uppercase, the function returns false.
  4. In the main function, two strings (str1 and str2) are tested and the result is printed with printf().

Example 2: Converting to Uppercase and Comparing

Description: In this example, we convert the input string to its uppercase equivalent using the toupper() function. Then, we compare the converted string with the original using strcmp() to determine if the original string was entirely uppercase.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>
#include <string.h>

// Function to convert a string to uppercase
void toUpperCase(char *dest, const char *src) {
    int i = 0;
    while (src[i] != '\0') {
        dest[i] = toupper(src[i]);
        i++;
    }
    dest[i] = '\0';
}

int main() {
    char str1[] = "GOODBYE";
    char str2[] = "Goodbye";
    char upperStr[100];

    // Convert str1 to uppercase and compare with original
    toUpperCase(upperStr, str1);
    if (strcmp(str1, upperStr) == 0) {
        printf("'%s' is uppercase.\n", str1);
    } else {
        printf("'%s' is not uppercase.\n", str1);
    }

    // Convert str2 to uppercase and compare with original
    toUpperCase(upperStr, str2);
    if (strcmp(str2, upperStr) == 0) {
        printf("'%s' is uppercase.\n", str2);
    } else {
        printf("'%s' is not uppercase.\n", str2);
    }

    return 0;
}

Output:

'GOODBYE' is uppercase.
'Goodbye' is not uppercase.

Explanation:

  1. The helper function toUpperCase converts each character of the input string src to its uppercase equivalent using toupper() and stores it in dest.
  2. In the main function, two strings (str1 and str2) are defined for testing.
  3. The toUpperCase function is used to generate an uppercase version of each string, which is stored in upperStr.
  4. The strcmp() function compares the original string with its uppercase version; if they match, the string is entirely uppercase.

Conclusion

In this tutorial, we explored two different approaches to check if a string is uppercase in C. The first approach uses the isupper() function to inspect each character, while the second approach converts the string to uppercase and compares it with the original.