Skip to content
Tutorials Center
My learning

Topics

Lesson 167 of 171 ยท Practice and Review

C++ Programs: String Tasks

C++ string programs with output: add binary strings, strip leading zeros, split a string, pangram check, reverse with a stack and word initials.

Strings are where a language's habits show. In C++ a std::string is a mutable sequence of char with methods for searching, slicing and appending, and the standard library supplies algorithms, streams and std::string_view to work on it. The programs below are small tasks that come up again and again: adding two numbers stored as text, trimming zeros, splitting on a delimiter, testing whether a sentence uses every letter, reversing with a stack and building an acronym.

Each program is complete and runnable. For the reference material behind them, see strings, string methods, string streams and string_view.

Character classification functions such as std::isalpha and std::toupper take an int that must be representable as unsigned char (or EOF). Passing a negative char is undefined behaviour, so these programs cast with static_cast<unsigned char>(ch) first.

1. Add two binary strings

Add from the right, one digit at a time, carrying like schoolbook addition. Because the digits arrive from the least significant end, collect them backwards and reverse once at the end. This works for numbers far longer than any integer type.

C++ 1. Add binary strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The expression ch - '0' turns a digit character into its numeric value because the digit characters are consecutive in every character set C++ supports.

2. Remove leading zeros

find_first_not_of('0') returns the position of the first character that is not a zero, or npos if the string is all zeros. Handle that case separately so that "0000" becomes "0" instead of an empty string.

C++ 2. Strip leading zeros

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

3. Split a string on a delimiter

Two common ways. A std::getline loop over a std::istringstream works with any single-character delimiter. A manual loop with find also handles multi-character separators and avoids the stream. Both keep empty fields between adjacent delimiters, which is usually what you want for data such as CSV.

C++ 3. Split a string

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Note the quirk of the stream version: a trailing delimiter ("a,b,") does not produce a final empty field, while the find version does. Pick the behaviour your data needs. C++20 also offers std::views::split for lazy splitting; see the ranges lesson.

4. Pangram check

A pangram contains every letter of the alphabet at least once. Keep a 26-bit mask (a std::bitset<26> or an unsigned) and set one bit per letter seen, ignoring case and non-letters.

C++ 4. Pangram

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

5. Reverse a string with a stack

A stack gives back items in the opposite order they went in, which is exactly what reversing means. This is a teaching example (in real code use std::ranges::reverse or std::reverse), but the same idea reverses words, matches brackets and undoes edits.

C++ 5. Reverse with a stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

6. First letters of each word

Walk the characters; a letter that follows a space (or starts the string) begins a new word, so append it to the result. Skipping runs of spaces falls out of the same rule. Upper-casing the result gives an acronym.

C++ 6. Initials

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do the binary-addition program's digits get reversed at the end?

    The loop starts at the right ends of the inputs, so the result is built in reverse order and reversed once at the end.
  2. 2. What does s.find_first_not_of('0') return for the string "0000"?

    No character differs from '0', so the function returns npos. The program must handle this case explicitly.
  3. 3. How does splitting "a,b," with std::getline and a comma differ from the find-based loop?

    getline stops at end of input without producing a last empty field, while the find loop appends the remainder, which is empty.
  4. 4. Why cast a char to unsigned char before calling std::isalpha?

    The <cctype> functions require values representable as unsigned char (or EOF); a negative char would break that rule.

Summary

  • Add digit strings from the right with a carry and reverse the result once.
  • find_first_not_of strips leading characters; check for npos so an all-zero string stays "0".
  • Split with getline for single-character delimiters or a find loop for longer separators, and know how each handles empty fields.
  • A bitmask records which letters were seen, which makes pangram checks one pass.
  • A stack reverses order; prefer std::reverse in real code.
  • Cast char to unsigned char before <cctype> calls.

Next: C++ Programs: OOP and STL Recipes.