#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.
*/