SyntaxPilot
Courses/C Coding Questions/Lesson 16
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

C Coding Questions · Lesson 16 of 30

String questions II

count vowelsconsonantsdigits and words; toggle case; remove all spaces in place; fgets and the newline

These are the notes for C Coding Questions, episode 16, placement practice for people who finished the SyntaxPilot C course. String questions, part two: counting vowels, consonants, digits and words in a line, toggling the case of every letter, and removing every space in place. On the way: why scanf("%s") reads only one word, and how to remove the newline that fgets keeps.

Each question follows the same steps: read the problem, think out loud, the obvious attempt, a real failure, why, the fix, every test case, and the complexity.

Every program was compiled with gcc -Wall -O2 -Wno-unused-result main.c -o main -lm (gcc 13.3, Linux x86-64, 32-bit int), the SyntaxPilot compiler's C command with warnings on, and every output below is real. In the input lines below, ⏎ means pressing Enter. Timings are from our machine; yours will differ, but the gap between O(n) and O(n²) won't.

Real-life pictures

In real life In C
a shopkeeper sorts the change into boxes: ones, twos, fives, tens, looking at each coin once count by kind: look at each character once, ask "letter? vowel? digit?"
you look up and Caps Lock was on: hELLO wORLD toggle case: small ↔ capital, nothing else changes
a form rejects 98765 43210 because of the space remove all spaces in place: 9876543210
counting train coaches: count each coach where it starts, not the gaps count words where they start, with a flag

First: reading a whole line

scanf("%s", s) reads one word: it skips leading whitespace, then stops at the next whitespace character (a space, a tab or a newline: characters you can't see). With no width it can also write past the end of s if the word is long; for char s[100] write %99s.

fgets(s, sizeof s, stdin) reads a whole line, spaces included, keeps the '\n' from the Enter key (if it fits) and adds '\0'. It returns NULL at the end of the input.

To remove the newline, use strcspn from <string.h>: strcspn(s, "\n") is the index of the first '\n' in s, or the length of s if there is none. So this one line removes it if it is there and does nothing otherwise, and it never goes out of bounds:

s[strcspn(s, "\n")] = '\0';

(Episode 15 used if (n > 0 && s[n - 1] == '\n') s[--n] = '\0';, which does the same when you already know the length n.)

Q1: Count vowels, consonants and digits

Problem. Read a line and print how many vowels (a, e, i, o, u, small or capital), consonants (any other letter) and digits it has. Spaces and punctuation are neither.

Think out loud.

  • Input: a line of text. Output: three counts.
  • Hello World 2026 → vowels 3 (e, o, o), consonants 7 (H, l, l, W, r, l, d), digits 4.
  • AEIOU → vowels 5, consonants 0, digits 0.
  • Edge cases: spaces, capital letters, digits, the '\n' from fgets, an empty line.

The obvious attempt: scanf("%s")

#include <stdio.h>

int main() {
    char s[100];
    scanf("%s", s);
    int v = 0, c = 0, d = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        char ch = s[i];
        if (ch == 'a' || ch == 'e' || ch == 'i' ||
            ch == 'o' || ch == 'u') v++;
        else if (ch >= '0' && ch <= '9') d++;
        else c++;
    }
    printf("\"%s\": vowels %d, consonants %d, digits %d\n", s, v, c, d);
    return 0;
}

Input Hello World 2026⏎ AEIOU⏎:

"Hello": vowels 2, consonants 3, digits 0

Why. %s stops at the first space, so only Hello was read. (Even for Hello the count is wrong: capital H is a consonant, but so would capital E be.)

Second attempt: fgets, small vowels, "else is a consonant"

#include <stdio.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        int v = 0, c = 0, d = 0;
        for (int i = 0; s[i] != '\0'; i++) {
            char ch = s[i];
            if (ch == 'a' || ch == 'e' || ch == 'i' ||
                ch == 'o' || ch == 'u') v++;
            else if (ch >= '0' && ch <= '9') d++;
            else c++;
        }
        printf("vowels %d, consonants %d, digits %d\n", v, c, d);
    }
    return 0;
}

Same input:

vowels 3, consonants 10, digits 4
vowels 0, consonants 6, digits 0

Why. else c++ counts everything that isn't a small vowel or a digit: the two spaces and the '\n' (7 + 3 = 10). And 'A' is not 'a', so AEIOU became 5 consonants plus the newline (6).

The fix: strip the newline, then ask the right questions

  • s[strcspn(s, "\n")] = '\0'; removes the newline.
  • isalpha(ch) (from <ctype.h>) is true for a letter. Only letters can be vowels or consonants.
  • tolower(ch) gives the small letter, so capitals are handled.
  • strchr("aeiou", c) (from <string.h>) returns a pointer to c in "aeiou", or NULL if it isn't there. Found: a vowel; not found: a consonant.
  • isdigit(ch) is true for '0'..'9'. Anything else is not counted.
  • The char is stored in an unsigned char: the <ctype.h> functions need a value that is EOF or fits in unsigned char; a negative char (possible for non-ASCII text) is undefined behaviour.
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        int v = 0, c = 0, d = 0;
        for (int i = 0; s[i] != '\0'; i++) {
            unsigned char ch = s[i];
            if (isalpha(ch)) {
                if (strchr("aeiou", tolower(ch))) v++;
                else c++;
            } else if (isdigit(ch)) d++;
        }
        printf("\"%s\": vowels %d, consonants %d, digits %d\n",
               s, v, c, d);
    }
    return 0;
}

Same input:

"Hello World 2026": vowels 3, consonants 7, digits 4
"AEIOU": vowels 5, consonants 0, digits 0

More tests, Priya, 21, Pune!⏎ ⏎ 12345⏎:

"Priya, 21, Pune!": vowels 4, consonants 5, digits 2
"": vowels 0, consonants 0, digits 0
"12345": vowels 0, consonants 0, digits 5

Every test passes. Time O(n): one look at each character (strchr on a 5-letter string is constant work). Space O(1): three counters.

Q1, part two: Count the words

Problem. Count the words in each line. A word is a run of characters with no whitespace in it.

Think out loud.

  • Hello World 2026 → 3, SyntaxPilot → 1.
  • ␣␣chai␣␣␣time␣ (spaces at the start, three in the middle, one at the end) → 2.
  • An empty line → 0.

The obvious attempt: spaces + 1

#include <stdio.h>
#include <string.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        int words = 1;
        for (int i = 0; s[i] != '\0'; i++)
            if (s[i] == ' ') words++;
        printf("\"%s\": %d words\n", s, words);
    }
    return 0;
}

Input Hello World 2026⏎ ␣␣chai␣␣␣time␣⏎ ⏎ SyntaxPilot⏎:

"Hello World 2026": 3 words
"  chai   time ": 7 words
"": 1 words
"SyntaxPilot": 1 words

Why. "Spaces + 1" is only right with exactly one space between words and none at the ends. ␣␣chai␣␣␣time␣ has 6 spaces → 7; the empty line has none → 1.

The fix: count where a word starts

A word starts at a non-space character that comes after a space (or at the very beginning). Keep a flag in_word (1 while we are inside a word): a space sets it to 0; a non-space while in_word is 0 starts a new word.

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

int count_words(const char s[]) {
    int words = 0, in_word = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        if (isspace((unsigned char)s[i])) in_word = 0;
        else if (!in_word) { in_word = 1; words++; }
    }
    return words;
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        printf("\"%s\": %d words\n", s, count_words(s));
    }
    return 0;
}

Same input:

"Hello World 2026": 3 words
"  chai   time ": 2 words
"": 0 words
"SyntaxPilot": 1 words

Tabs too, because isspace is true for ' ', '\t', '\n' and a few more. Input one\ttwo three⏎ (a tab and two spaces):

"one    two  three": 3 words

Time O(n), space O(1): a counter and a flag.

Q2: Toggle the case

Problem. Every small letter becomes capital and every capital becomes small; everything else stays.

Think out loud.

  • Hello World 2026 → hELLO wORLD 2026; the Caps Lock line sYNTAXpILOT → SyntaxPilot.
  • In ASCII, 'a' is 97 and 'A' is 65: every small letter is exactly 32 more than its capital.
  • Edge cases: spaces, digits and punctuation must not change.

The obvious attempt: subtract 32 from everything

#include <stdio.h>
#include <string.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        for (int i = 0; s[i] != '\0'; i++)
            s[i] = s[i] - 32;
        printf("%s\n", s);
    }
    return 0;
}

Input Hello World 2026⏎ sYNTAXpILOT⏎:

(ELLO
S9.4!8P),/4

Why. - 32 is only right for small letters. 'H' (72) − 32 = 40 = '('. And the space is 32: 32 − 32 = 0 = '\0', so the string ends after (ELLO. In the second line the capitals became digits and symbols ('Y' 89 − 32 = 57 = '9').

Second attempt: check the range, with two separate ifs

#include <stdio.h>
#include <string.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        for (int i = 0; s[i] != '\0'; i++) {
            if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 32;
            if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] + 32;
        }
        printf("%s\n", s);
    }
    return 0;
}

Same input:

hello world 2026
syntaxpilot

Why. Both ifs run on the same character. 'e' (101) is small, so it becomes 'E' (69); then the second if sees a capital and adds 32 back: 'e' again. Small letters go up and straight back down; only capitals really change, so everything comes out small.

The fix: else if (and the ctype functions)

With else if only one of the two changes can happen. islower/isupper ask the question and toupper/tolower do the change, which is clearer than magic numbers:

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

void toggle(char s[]) {
    for (int i = 0; s[i] != '\0'; i++) {
        unsigned char ch = s[i];
        if (islower(ch)) s[i] = toupper(ch);
        else if (isupper(ch)) s[i] = tolower(ch);
    }
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        toggle(s);
        printf("%s\n", s);
    }
    return 0;
}

Same input:

hELLO wORLD 2026
SyntaxPilot

Time O(n), space O(1) (in place).

The XOR trick: for an ASCII letter, c ^ 32 flips its case, because 32 is the one bit that differs between 'a' and 'A'. Only use it after checking isalpha, or it changes spaces and digits too.

Q3: Remove all spaces, in place

Problem. Delete every ' ' from the line, in place (inside the same array, no second array).

Think out loud.

  • New Delhi → NewDelhi, I love chai → Ilovechai, 98765 43210 → 9876543210.
  • Edge cases: two spaces in a row, spaces at both ends, an empty line.

The obvious attempt: shift the rest left, then i++

#include <stdio.h>
#include <string.h>

void remove_spaces(char s[]) {
    for (int i = 0; s[i] != '\0'; i++) {
        if (s[i] == ' ') {
            for (int k = i; s[k] != '\0'; k++)
                s[k] = s[k + 1];
        }
    }
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        remove_spaces(s);
        printf("\"%s\"\n", s);
    }
    return 0;
}

Input New Delhi⏎ New␣␣Delhi⏎ ␣␣chai␣␣⏎ I love chai⏎ ⏎:

"NewDelhi"
"New Delhi"
" chai "
"Ilovechai"
""

Why. In New␣␣Delhi, at i = 3 there is a space; shifting left moves the second space into position 3. Then i++ moves on to position 4 and the space now at 3 is never checked.

Changing if to while fixes the answer, but every removed space shifts the whole rest of the string. With about n/2 spaces, that is about (n/2) × (n/2) moves: O(n²). Measured:

#include <stdio.h>
#include <string.h>
#include <time.h>

#define N 200000
char a[N + 1], b[N + 1];

double secs(clock_t start) {
    return (double)(clock() - start) / CLOCKS_PER_SEC;
}

void shift_way(char s[]) {
    for (int i = 0; s[i] != '\0'; i++)
        while (s[i] == ' ')
            for (int k = i; s[k] != '\0'; k++)
                s[k] = s[k + 1];
}

void two_index_way(char s[]) {
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++)
        if (s[i] != ' ')
            s[j++] = s[i];
    s[j] = '\0';
}

int main() {
    for (int i = 0; i < N; i++) a[i] = b[i] = (i % 2) ? ' ' : 'x';
    clock_t t = clock();
    shift_way(a);
    printf("shift:     %zu chars left, %.2f s\n", strlen(a), secs(t));
    t = clock();
    two_index_way(b);
    printf("two index: %zu chars left, %.2f s\n", strlen(b), secs(t));
    return 0;
}

Output (timings from our machine):

shift:     100000 chars left, 2.26 s
two index: 100000 chars left, 0.00 s

The fix: a read index and a write index

i reads every character; j is where the next character we keep is written. If s[i] isn't a space, copy it to s[j] and move j (s[j++] = s[i] means: use j, then add 1). At the end, s[j] = '\0' ends the shorter string. j never gets ahead of i, so we never overwrite a character before reading it.

#include <stdio.h>
#include <string.h>

void remove_spaces(char s[]) {
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++)
        if (s[i] != ' ')
            s[j++] = s[i];
    s[j] = '\0';
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        remove_spaces(s);
        printf("\"%s\"\n", s);
    }
    return 0;
}

Same input:

"NewDelhi"
"NewDelhi"
"chai"
"Ilovechai"
""

And 98765 43210⏎:

"9876543210"

Each character is read once and written at most once: O(n) time, O(1) space. The same read/write-index trick removes duplicates from a sorted array (episode 9) and filters any array in place.

Complexity summary

Task Method Time Space
count vowels, consonants, digits scanf("%s") O(n), only the first word O(1)
else = consonant O(n), counts spaces and '\n' O(1)
isalpha + tolower + strchr, isdigit O(n) O(1)
count words spaces + 1 O(n), wrong with extra spaces O(1)
in_word flag O(n) O(1)
toggle case - 32 on every char O(n), breaks non-letters O(1)
two separate ifs O(n), everything small O(1)
else if, islower/toupper O(n) O(1)
remove spaces shift left (with while) O(n²) O(1)
read i, write j O(n) O(1)

Common mistakes (and the real results)

Mistake What you see Why Fix
scanf("%s", s) for a line no error: only "Hello" is read %s stops at whitespace fgets(s, sizeof s, stdin)
scanf("%s", s) with no width no warning; a long word overflows s (undefined behaviour) no limit on how much is written scanf("%99s", s) for char s[100]
counting after fgets without stripping no error: one char too many the '\n' is in the string s[strcspn(s, "\n")] = '\0';
else c++ for consonants no error: consonants 10 instead of 7 spaces and '\n' fall into else if (isalpha(ch)) first
checking only 'a'..'u' small no error: AEIOU has 0 vowels 'A' is not 'a' tolower(ch)
words = spaces + 1 no error: 7 words, "" has 1 extra spaces, empty line count word starts (in_word)
s[i] = s[i] - 32 on every char no error: (ELLO ' ' − 32 = '\0' check the range, islower
two separate ifs to toggle no error: all small flipped, then flipped back else if
shift left, then i++ no error: "New Delhi" the second space is skipped; O(n²) anyway read i, write j
forgetting s[j] = '\0' no error: 6 Cisfunun (the quiz) the old tail is still there end the string at j
isalpha without #include <ctype.h> warning: implicit declaration of function 'isalpha' not declared #include <ctype.h>

The missing header for real:

#include <stdio.h>

int main() {
    char c = 'x';
    printf("%d\n", isalpha(c) != 0);
    return 0;
}

Compiler output:

main.c: In function 'main':
main.c:5:20: warning: implicit declaration of function 'isalpha' [-Wimplicit-function-declaration]
    5 |     printf("%d\n", isalpha(c) != 0);
      |                    ^~~~~~~
main.c:2:1: note: include '<ctype.h>' or provide a declaration of 'isalpha'
    1 | #include <stdio.h>
  +++ |+#include <ctype.h>
    2 |

Your turn: capitalise every word (solution)

Problem. Make the first letter of every word capital and the rest small; keep the spaces as they are. i LOVE␣␣␣masala chai → I Love␣␣␣Masala Chai.

Idea. Q1's in_word flag tells you where a word starts; Q2's toupper/tolower do the change. At a word start: toupper; inside a word: tolower. O(n) time, O(1) space.

Show the solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void capitalise(char s[]) {
    int in_word = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        unsigned char c = s[i];
        if (isspace(c)) in_word = 0;
        else if (!in_word) { s[i] = toupper(c); in_word = 1; }
        else s[i] = tolower(c);
    }
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        capitalise(s);
        printf("\"%s\"\n", s);
    }
    return 0;
}

Input i LOVE masala chai⏎ ␣sYNTAXpILOT⏎ 2026 is here⏎:

"I Love   Masala Chai"
" Syntaxpilot"
"2026 Is Here"

A word starting with a digit keeps it (toupper('2') is '2').

Practice questions (with solutions)

1. Count spaces, tabs and punctuation too. Add counters for isspace (not counting the stripped newline) and ispunct (punctuation: ! , . ? and so on).

Solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        int letters = 0, digits = 0, spaces = 0, punct = 0;
        for (int i = 0; s[i] != '\0'; i++) {
            unsigned char c = s[i];
            if (isalpha(c)) letters++;
            else if (isdigit(c)) digits++;
            else if (isspace(c)) spaces++;
            else if (ispunct(c)) punct++;
        }
        printf("letters %d, digits %d, spaces %d, punctuation %d\n",
               letters, digits, spaces, punct);
    }
    return 0;
}

Input Hi! Call me at 9:30, ok?⏎:

letters 12, digits 3, spaces 5, punctuation 4

2. Remove all vowels from a line, in place. The Q3 read/write trick with a different test.

Solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void remove_vowels(char s[]) {
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++)
        if (!strchr("aeiou", tolower((unsigned char)s[i])))
            s[j++] = s[i];
    s[j] = '\0';
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        remove_vowels(s);
        printf("\"%s\"\n", s);
    }
    return 0;
}

Input SyntaxPilot Academy⏎ AEIOU⏎:

"SyntxPlt cdmy"
""

Careful: strchr(s, '\0') finds the terminator, so never call it with c == '\0'; inside this loop s[i] is never '\0'.

3. Squeeze many spaces into one. Keep a space only if the last character written isn't a space; also drop spaces at the start and the end.

Solution
#include <stdio.h>
#include <string.h>

void squeeze(char s[]) {
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        if (s[i] != ' ') s[j++] = s[i];
        else if (j > 0 && s[j - 1] != ' ') s[j++] = ' ';
    }
    if (j > 0 && s[j - 1] == ' ') j--;
    s[j] = '\0';
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        squeeze(s);
        printf("\"%s\"\n", s);
    }
    return 0;
}

Input ␣␣chai␣␣␣time␣⏎ New Delhi⏎ ␣␣␣⏎:

"chai time"
"New Delhi"
""

Quick quiz

#include <stdio.h>

int main() {
    char s[] = "C is fun";
    int j = 0;
    for (int i = 0; s[i] != '\0'; i++)
        if (s[i] != ' ') s[j++] = s[i];
    printf("%d %s\n", j, s);
    return 0;
}

(A) 6 Cisfun (B) 6 Cisfunun (C) 8 C is fun (D) 6 Cisfun fun

Answer

(B) 6 Cisfunun. Six chars (C i s f u n) are copied to the front, but s[j] = '\0' is missing. The old 'u' and 'n' at index 6 and 7 are still there, followed by the original '\0'.

6 Cisfunun

Interview questions

  1. Why does scanf("%s") read only one word? %s stops at whitespace. Use fgets for a line, and always give %s a width.
  2. How do you remove the newline from fgets? s[strcspn(s, "\n")] = '\0'; (safe even when there is no newline).
  3. Count vowels and consonants without counting spaces. Check isalpha first, then tolower and look for it in "aeiou".
  4. Count words when there can be many spaces. Count word starts with an in-word flag, not spaces + 1.
  5. Toggle case without <ctype.h>. Range checks with else if; or for a letter, c ^ 32.
  6. Why does s[i] - 32 on a whole string cut it short? The space (32) becomes '\0'.
  7. Remove all spaces in O(n). Read index and write index, then s[j] = '\0'.
  8. Why is deleting by shifting O(n²)? Every deletion moves the rest of the string.
  9. Why cast to unsigned char for <ctype.h> functions? A negative argument other than EOF is undefined behaviour.

Glossary

Term Meaning Example
Whitespace Characters you can't see: space, tab, newline ' ', '\t', '\n'
scanf("%s") Reads one word, stopping at whitespace reads Hello from Hello World
Width (%99s) The most chars %s may store (leave 1 for '\0') char s[100] → %99s
fgets Reads one line, keeps the '\n', adds '\0', returns NULL at the end fgets(s, sizeof s, stdin)
strcspn(s, set) Index of the first char of s that is in set, or the length strcspn("ab\n", "\n") = 2
strchr(s, c) Pointer to the first c in s, or NULL strchr("aeiou", 'o') is not NULL
Vowel a, e, i, o, u (small or capital) E in Hello
Consonant Any letter that isn't a vowel H, l, W
isalpha True for a letter isalpha('9') is false
isdigit True for '0'..'9' isdigit('7') is true
isspace True for space, tab, newline and similar isspace('\t') is true
ispunct True for punctuation ispunct('!') is true
islower / isupper True for a small / capital letter isupper('A') is true
tolower / toupper Small / capital version of a letter; other chars unchanged toupper('e') = 'E'
ASCII code The number that stores a character 'a' = 97, 'A' = 65, ' ' = 32
Toggle case Small ↔ capital for every letter Hello → hELLO
Flag A variable that remembers a yes/no state in_word
Word start A non-space after a space, or at the beginning c in ␣␣chai
else if Only the first true branch runs toggle: one change per char
In place Changed inside the same array, O(1) extra space remove spaces
Read/write index i reads every element, j writes the ones kept s[j++] = s[i]
s[j++] Use j, then add 1 to it (post-increment) write, then move on
clock() Processor time used so far, in CLOCKS_PER_SEC units timing the two ways
O(n) / O(n²) Work grows like n / like n × n one pass / shift for every space
Undefined behaviour (UB) The C standard makes no promise about the result scanf("%s") overflowing s
XOR (^) Bitwise exclusive or; c ^ 32 flips bit 5 'a' ^ 32 = 'A'

Summary

  • scanf("%s") reads one word; fgets reads the line, and s[strcspn(s, "\n")] = '\0' removes its newline.
  • Count by kind with isalpha, tolower + strchr("aeiou", …) and isdigit; count words where they start, with a flag.
  • Toggle only letters, with else if: s[i] - 32 turns the space into '\0', and two separate ifs undo each other.
  • Remove in place with a read index and a write index, then s[j] = '\0': O(n), where shifting is O(n²).

Next episode: String questions III: anagram check, first non-repeating character, frequency of every character.