Check if a String Follows Camel Case in C

To check if a string follows camel case in C, you can implement a function that verifies the first character is lowercase and that any uppercase letter marks the start of a new word.


Example 1: Using an Iterative Approach

In this example, we will iterate through each character of the string using a loop. We check that the first character is lowercase and that any uppercase letter encountered is immediately preceded by a lowercase letter, ensuring the string follows the camel case format.

main.c

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

int isCamelCase(const char *str) {
    if (str == NULL || str[0] == '\0')
        return 0; // Empty string is not valid

    // The first character must be lowercase
    if (!islower(str[0]))
        return 0;

    for (int i = 1; i < strlen(str); i++) {
        // Ensure each character is alphabetic
        if (!isalpha(str[i]))
            return 0;
        // If the character is uppercase, its previous character should be lowercase
        if (isupper(str[i]) && !islower(str[i - 1]))
            return 0;
    }
    return 1;
}

int main() {
    char test1[] = "camelCaseExample";
    char test2[] = "CamelCaseExample";
    char test3[] = "camelcaseexample";
    char test4[] = "camelCASEExample";

    printf("'%s' is %scamelCase\n", test1, isCamelCase(test1) ? "" : "not ");
    printf("'%s' is %scamelCase\n", test2, isCamelCase(test2) ? "" : "not ");
    printf("'%s' is %scamelCase\n", test3, isCamelCase(test3) ? "" : "not ");
    printf("'%s' is %scamelCase\n", test4, isCamelCase(test4) ? "" : "not ");
    
    return 0;
}

Explanation:

  1. The function isCamelCase first checks if the string is empty or NULL.
  2. It then verifies that the first character is lowercase using islower().
  3. A for loop iterates over the remaining characters, ensuring each is an alphabet using isalpha().
  4. For every uppercase character found (using isupper()), it checks that the preceding character is lowercase.
  5. The main function tests multiple strings and prints whether each follows camel case.

Output:

'camelCaseExample' is camelCase
'CamelCaseExample' is not camelCase
'camelcaseexample' is camelCase
'camelCASEExample' is not camelCase

Example 2: Using POSIX Regular Expressions

In this example, we use POSIX regular expressions to verify the camel case pattern. The regex pattern ensures the string starts with one or more lowercase letters, followed by zero or more groups of an uppercase letter and one or more lowercase letters.

main.c

</>
Copy
#include <stdio.h>
#include <regex.h>

int isCamelCaseRegex(const char *str) {
    regex_t regex;
    // Regex pattern: one or more lowercase letters followed by zero or more groups of one uppercase letter and one or more lowercase letters
    const char *pattern = "^[a-z]+([A-Z][a-z]+)*$";
    int ret;

    ret = regcomp(&regex, pattern, REG_EXTENDED);
    if (ret) {
        fprintf(stderr, "Could not compile regex\n");
        return 0;
    }

    ret = regexec(&regex, str, 0, NULL, 0);
    regfree(&regex);

    return (ret == 0);
}

int main() {
    char test1[] = "regexCamelCase";
    char test2[] = "RegexCamelCase";
    char test3[] = "regexcamelcase";

    printf("'%s' is %scamelCase (using regex)\n", test1, isCamelCaseRegex(test1) ? "" : "not ");
    printf("'%s' is %scamelCase (using regex)\n", test2, isCamelCaseRegex(test2) ? "" : "not ");
    printf("'%s' is %scamelCase (using regex)\n", test3, isCamelCaseRegex(test3) ? "" : "not ");

    return 0;
}

Explanation:

  1. The function isCamelCaseRegex compiles a regex pattern using regcomp() from the POSIX regex library.
  2. The regex pattern ^[a-z]+([A-Z][a-z]+)*$ ensures the string starts with one or more lowercase letters and may be followed by groups that start with an uppercase letter followed by lowercase letters.
  3. The regexec() function checks if the string matches the pattern.
  4. If the string matches, the function returns 1 (true); otherwise, it returns 0 (false).
  5. The main function tests multiple strings and displays the result using the regex approach.

Output:

'regexCamelCase' is camelCase (using regex)
'RegexCamelCase' is not camelCase (using regex)
'regexcamelcase' is camelCase (using regex)

Conclusion

In this tutorial, we explored two approaches to check if a string follows camel case in C:

  1. Iterative Approach: Uses a loop and character functions from ctype.h to verify that the string starts with a lowercase letter and that uppercase letters are properly preceded by lowercase letters.
  2. Regex Approach: Utilizes POSIX regular expressions to match the camel case pattern, ensuring the string meets the required format.