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
#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:
- The function
isStringUppercase
iterates over each character of the input stringstr
using awhile
loop. - It uses
isalpha()
to check if a character is a letter, andisupper()
to verify if that letter is in uppercase. - If any alphabetic character is not uppercase, the function returns
false
. - In the
main
function, two strings (str1
andstr2
) are tested and the result is printed withprintf()
.
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
#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:
- The helper function
toUpperCase
converts each character of the input stringsrc
to its uppercase equivalent usingtoupper()
and stores it indest
. - In the
main
function, two strings (str1
andstr2
) are defined for testing. - The
toUpperCase
function is used to generate an uppercase version of each string, which is stored inupperStr
. - 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.