Skip to content

Processing text

Read · 1 of 5

Chars are small numbers

Letters have codes in order: 'a' to 'z' are consecutive, so are 'A' to 'Z' and '0' to '9'. c >= 'a' && c <= 'z' tests for a lowercase letter, and c - '0' turns the digit character '7' into the number 7.

Most text problems are one loop from position 0 to the '\0', looking at each char.

Read this lesson as text

Count, transform and check strings character by character.

Chars are small numbers

Letters have codes in order: 'a' to 'z' are consecutive, so are 'A' to 'Z' and '0' to '9'. c >= 'a' && c <= 'z' tests for a lowercase letter, and c - '0' turns the digit character '7' into the number 7.

Most text problems are one loop from position 0 to the '\0', looking at each char.

Exercise: Count the vowels

Read a line (up to 200 characters) and print how many vowels (a, e, i, o, u, upper or lower case) it has.

Exercise: Palindrome

Read one word (lowercase, under 100 letters). Print Yes if it reads the same backwards, otherwise No.

Exercise: Shout it

Read a line (up to 200 characters) and print it in capitals, changing only the letters a to z, without using toupper.

Exercise: Count the words

Read a line (up to 200 characters). Words are separated by one or more spaces, and the line may start or end with spaces. Print the number of words.