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

</>
Copy
#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:

  1. The function isAllLowercase receives a constant character pointer str representing the input string.
  2. A while loop iterates through each character of the string until the null-terminator '\0' is reached.
  3. 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 returns 0 (false).
  4. If the loop completes without encountering any uppercase letters, the function returns 1 (true), indicating that the string is all lowercase.
  5. 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

</>
Copy
#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:

  1. The function isLowercaseByComparison takes a constant character pointer str as input.
  2. A temporary array lowerStr is created to store the lowercase version of the input string.
  3. A while loop iterates over the characters of str, converting each one to lowercase using tolower() and storing it in lowerStr.
  4. The loop ensures that no more than 99 characters are processed to prevent buffer overflow, and the resulting string is null-terminated.
  5. The original string str is compared to lowerStr using strcmp(). If they match, the function returns 1 (true), indicating the string is all lowercase; otherwise, it returns 0 (false).
  6. The main function calls isLowercaseByComparison 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.