Check if a String is Lowercase in C
To check whether a string is entirely lowercase in C, you can either examine each character using functions like islower()
from <ctype.h>
or convert the string to lowercase with tolower()
and compare it to the original.
Example 1: Using islower()
to Check Each Character
This example demonstrates how to iterate through a string character by character and use the islower()
function to determine if each letter is lowercase. Non-letter characters, such as spaces, are allowed and ignored.
main.c
#include <stdio.h>
#include <ctype.h>
int isAllLowercase(const char *str) {
while (*str) {
// If the character is an uppercase letter, return false (0)
if (*str >= 'A' && *str <= 'Z')
return 0;
str++;
}
return 1;
}
int main() {
const char *text = "hello world";
if (isAllLowercase(text))
printf("The string is all lowercase.\n");
else
printf("The string is not all lowercase.\n");
return 0;
}
Explanation:
- The function
isAllLowercase
receives a constant character pointerstr
representing the input string. - A
while
loop iterates through each character of the string until the null-terminator'\0'
is reached. - Inside the loop, we check if the current character is between
'A'
and'Z'
(i.e., an uppercase letter). If it is, the function immediately returns0
(false). - If the loop completes without encountering any uppercase letters, the function returns
1
(true), indicating that the string is all lowercase. - The
main
function tests this logic with the string"hello world"
and prints the appropriate message based on the result.
Output:
The string is all lowercase.
Example 2: Converting to Lowercase and Comparing Strings
This example illustrates how to check if a string is lowercase by converting it entirely to lowercase using the tolower()
function and then comparing the result with the original string. If both strings match, the original string was already all lowercase.
main.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int isLowercaseByComparison(const char *str) {
char lowerStr[100];
int i = 0;
// Convert each character of the input string to lowercase
while (str[i] != '\0' && i < 99) {
lowerStr[i] = tolower(str[i]);
i++;
}
lowerStr[i] = '\0'; // Null-terminate the converted string
// Compare the original string with the lowercase version
return (strcmp(str, lowerStr) == 0);
}
int main() {
const char *text = "hello world";
if (isLowercaseByComparison(text))
printf("The string is all lowercase.\n");
else
printf("The string is not all lowercase.\n");
return 0;
}
Explanation:
- The function
isLowercaseByComparison
takes a constant character pointerstr
as input. - A temporary array
lowerStr
is created to store the lowercase version of the input string. - A
while
loop iterates over the characters ofstr
, converting each one to lowercase usingtolower()
and storing it inlowerStr
. - The loop ensures that no more than 99 characters are processed to prevent buffer overflow, and the resulting string is null-terminated.
- The original string
str
is compared tolowerStr
usingstrcmp()
. If they match, the function returns1
(true), indicating the string is all lowercase; otherwise, it returns0
(false). - The
main
function callsisLowercaseByComparison
with the test string"hello world"
and prints the appropriate message based on the function’s result.
Output:
The string is all lowercase.
Conclusion
In this tutorial, we explored two approaches to check if a string is entirely lowercase in C. The first example uses the islower()
function to individually assess each character, while the second example converts the string to lowercase and compares it with the original.