How to check if a string is title case in C

1 Answer

0 votes
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>

bool is_title_case(const char *s) {
    bool new_word = true;

    for (; *s; s++) {
        unsigned char c = (unsigned char)*s;

        if (isspace(c)) {
            new_word = true;
        } else {
            if (new_word) {
                if (!isupper(c))
                    return false;
                new_word = false;
            } else {
                if (!islower(c))
                    return false;
            }
        }
    }
    
    return true;
}

int main(void) {
    printf("%d\n", is_title_case("Hello World"));   // 1
    printf("%d\n", is_title_case("Hello world"));   // 0
    printf("%d\n", is_title_case("hello World"));   // 0
}



/*
run:

1
0
0

*/

 



answered 7 hours ago by avibootz
...