Characters and Strings
Learn to work with letters and words — unlocking a whole new category of CP problems!
Open interactive version (quiz + challenge)Real-world analogy
If numbers are like math class, characters and strings are like English class! A character (char) is a single letter, like one Scrabble tile. A string is a word or sentence — a row of Scrabble tiles placed together. Just like you can look at the 3rd tile in a word, you can access the 3rd character in a string using an index!
What is it?
Characters (char) are single letters, digits, or symbols stored as numbers using ASCII encoding. Strings are sequences of characters that represent words or text. In C++, strings can be accessed character-by-character using array-style indexing.
Real-world relevance
Every app that deals with text uses strings — search engines finding words in web pages, spell checkers comparing your typing against a dictionary, password validators checking if you used uppercase and digits. Even emoji are characters with special numeric codes!
Key points
- What is a Character (char)? — A char in C++ stores a single letter, digit, or symbol. You write it with single quotes: char c = 'A'; Characters are actually stored as numbers using the ASCII table. 'A' is 65, 'B' is 66, 'a' is 97, '0' is 48. You can do math with characters! 'A' + 1 gives 'B'.
- What is a String? — A string is a sequence of characters — basically a word or sentence. In C++, use the string type: string s = "hello"; Strings use double quotes (not single). You can read a string with cin >> s (reads one word) or getline(cin, s) (reads a whole line including spaces).
- String Length and Indexing — s.length() or s.size() tells you how many characters are in the string. You can access individual characters with s[0], s[1], etc., just like an array! The first character is s[0] and the last is s[s.length()-1]. Strings are basically arrays of characters.
- The ASCII Table — ASCII is how computers store characters as numbers. Key ranges: 'A' to 'Z' are 65-90, 'a' to 'z' are 97-122, '0' to '9' are 48-57. The difference between uppercase and lowercase is always 32. So 'a' - 'A' = 32. This is super useful for converting between cases!
- Useful Character Functions — C++ has handy functions for characters: islower(c) checks if lowercase, isupper(c) checks uppercase, isdigit(c) checks if it is a digit, isalpha(c) checks if it is a letter. toupper(c) converts to uppercase, tolower(c) converts to lowercase. These save you from memorizing ASCII values!
- Looping Through a String — You can loop through every character in a string just like an array: for(int i = 0; i < s.length(); i++) works great. Or use a range-based for loop: for(char c : s) which gives you each character one at a time. This is how you process strings character by character.
- String Concatenation and Comparison — You can join strings with +: string result = "hello" + " " + "world"; You can compare strings with ==, . Strings are compared letter by letter (like dictionary order). "apple" < "banana" is true because 'a' comes before 'b'. This is called lexicographic comparison.
- Reading Strings with Spaces — cin >> s only reads until the first space. To read a whole line (with spaces), use getline(cin, s). Warning: if you use cin >> before getline, you need to add cin.ignore() in between to eat the leftover newline character. This is a common gotcha!
Code example
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s; // Read one word
cout << "Length: " << s.length() << endl;
cout << "First char: " << s[0] << endl;
cout << "Last char: " << s[s.length() - 1] << endl;
// Count uppercase and lowercase letters
int upper = 0, lower = 0, digits = 0;
for (char c : s) {
if (isupper(c)) upper++;
else if (islower(c)) lower++;
else if (isdigit(c)) digits++;
}
cout << "Uppercase: " << upper << endl;
cout << "Lowercase: " << lower << endl;
cout << "Digits: " << digits << endl;
// Convert to uppercase
string upperStr = s;
for (int i = 0; i < upperStr.length(); i++) {
upperStr[i] = toupper(upperStr[i]);
}
cout << "Uppercase version: " << upperStr << endl;
return 0;
}
// Input: Hello123World
// Output: Length: 13
// First char: H
// Last char: d
// Uppercase: 2
// Lowercase: 8
// Digits: 3
// Uppercase version: HELLO123WORLDLine-by-line walkthrough
- 1. We read a single word into string s using cin >>.
- 2. s.length() tells us how many characters are in the string.
- 3. s[0] is the first character, s[s.length()-1] is the last — just like arrays!
- 4. We use a range-based for loop (for char c : s) to check each character.
- 5. isupper(), islower(), isdigit() are built-in functions that check character types.
- 6. To convert the string to uppercase, we loop through and apply toupper() to each character.
- 7. toupper() converts a lowercase letter to uppercase. If it is already uppercase or a digit, it stays the same.
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
getline(cin, s);
cout << "You entered: " << s << endl;
cout << "Length: " << s.length() << endl;
return 0;
}
// Input: 3
// Hello World
// Expected: You entered: Hello World
// Length: 11
// Actual: You entered:
// Length: 0Need a hint?
When you use cin >> n, it reads the number but leaves the newline character in the input buffer. What does getline read first?
Show answer
The bug is the missing cin.ignore() between cin >> n and getline()! After cin >> n reads '3', the newline character '\n' is still in the input. getline() then reads that leftover newline as an empty string! The fix is to add cin.ignore(); right after cin >> n to consume the leftover newline before getline reads the actual string.
Explain like I'm 5
Think of a string like a friendship bracelet made of letter beads. Each bead is one character. You can count the beads (how long is it?), look at the 3rd bead from the left (which letter is there?), or check if a bead is a vowel. You can even make a new bracelet by changing all the beads to uppercase versions — it is like making the bracelet in ALL CAPS!
Fun fact
ASCII stands for 'American Standard Code for Information Interchange' and was created in 1963. The original ASCII only had 128 characters — just English letters, numbers, and basic symbols. Today, Unicode has over 150,000 characters covering every language on Earth, including ancient Egyptian hieroglyphics and yes, all those emoji you love!
Hands-on challenge
Read a string and print: (1) the number of vowels (a, e, i, o, u — both upper and lowercase), (2) the number of consonants, (3) the string with the first letter of each word capitalized (if you are feeling adventurous!). Hint: use tolower() to normalize before checking vowels.
More resources
- Strings in C++ — Full Course (CodeWithHarry)
- C++ Strings — cppreference (cppreference)
- String Problems for Beginners (Codeforces)