Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to check whether a word is an ABC word in C

1 Answer

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

/*
    Function: isABCWord
    Purpose:
        Determine whether the letters 'a', 'b', and 'c' appear in the word
        in alphabetical order (a → b → c). They do NOT need to be consecutive,
        only in the correct order of appearance.

    Method:
        - Scan the word once (O(n)).
        - Convert each character to lowercase for case-insensitive comparison.
        - Track whether 'a' has been seen, then 'b', then 'c'.
        - If 'b' appears before 'a', or 'c' appears before 'b', the order is invalid.
        - If we eventually see 'c' after both 'a' and 'b', return true.

    Notes:
        - Uses only standard C library functions.
        - Efficient and idiomatic: simple loop, no unnecessary memory or operations.
*/
int isABCWord(const char *word) {
    int seenA = 0;
    int seenB = 0;

    while (*word) {
        char c = tolower((unsigned char)*word);

        if (c == 'a') {
            seenA = 1;
        }
        else if (c == 'b') {
            if (!seenA) return 0;   /* 'b' before 'a' → invalid */
            seenB = 1;
        }
        else if (c == 'c') {
            if (!seenB) return 0;   /* 'c' before 'b' → invalid */
            return 1;               /* Found a → b → c in order */
        }

        word++;
    }

    return 0;   /* Did not find all three in order */
}

int main(void) {
    const char *word = "algebraic";

    printf("Word: %s\n", word);

    if (isABCWord(word)) {
        printf("Result: This IS an ABC word.\n");
    } else {
        printf("Result: This is NOT an ABC word.\n");
    }

    return 0;
}


/*
run:

Word: algebraic
Result: This IS an ABC word.

*/

 



answered Jul 10 by avibootz
...