Chapter 05 of 30
Strings & Character Operations
Deep dive into std::string methods, character manipulation, and ASCII-based tricks for competitive programming.
std::string in C++Character Operations & ASCII Mathematics
Share:
Strings & Character Operations
Strings appear in everything from basic I/O to advanced dynamic programming. This chapter goes deeper into std::string methods and the ASCII-based tricks that make character manipulation fast.
std::string in C++
Construction
hljs cpp
string s1; // empty string
string s2 = "hello"; // from C-string literal
string s3(5, 'a'); // "aaaaa"
string s4 = s2.substr(1, 3); // "ell"
string s5(s2); // copy of s2
Essential Methods
hljs cpp
s.length(); // number of characters (use s.size() interchangeably)
s.empty(); // true if length is 0
s.clear(); // makes the string empty
// Access
s[0]; // first character (no bounds checking!)
s.at(0); // first character (throws if out of bounds)
s.front(); // first character
s.back(); // last character
// Modification
s.push_back('x'); // append character
s += " world"; // append string
s.insert(2, "!!"); // insert at position 2
s.erase(1, 3); // erase 3 characters starting at position 1
s.replace(0, 2, "Hi"); // replace 2 chars at position 0 with "Hi"
// Search
s.find("lo"); // index of first occurrence (string::npos if not found)
s.rfind("l"); // index of last occurrence
s.find_first_of("aeiou"); // index of first vowel
s.find_first_not_of(" "); // index of first non-space
// Comparison
s1 == s2; // equality
s1 != s2; // inequality
s1 < s2; // lexicographic comparison
s1.compare(s2); // returns 0 if equal, negative if s1 < s2, positive if s1 > s2
Iterating
hljs cpp
// Range-based for (read-only)
for (char c : s) {
cout << c;
}
// Range-based for (modifiable)
for (char &c : s) {
c = toupper(c);
}
// Index-based
for (int i = 0; i < (int)s.size(); i++) {
cout << s[i];
}
Conversion
hljs cpp
// Number to string
string s = to_string(42); // "42"
string s = to_string(3.14); // "3.140000"
// String to number
int n = stoi("123"); // 123
long long ll = stoll("123456789012345"); // 123456789012345
double d = stod("3.14"); // 3.14
Character Operations & ASCII Mathematics
ASCII Table (Key Values)
| Character | ASCII | Character | ASCII |
|---|---|---|---|
'0' | 48 | ' ' (space) | 32 |
'A' | 65 | 'a' | 97 |
'Z' | 90 | 'z' | 122 |
Useful Conversions
hljs cpp
// Lowercase to uppercase
char upper = lowercase - 32; // or: toupper(lowercase)
// Uppercase to lowercase
char lower = uppercase + 32; // or: tolower(uppercase)
// Character to digit
int d = c - '0'; // '5' - '0' = 5
// Digit to character
char c = d + '0'; // 5 + '0' = '5'
Character Classification
hljs cpp
#include <cctype>
isalpha(c); // true if letter
isdigit(c); // true if digit
isalnum(c); // true if letter or digit
isspace(c); // true if whitespace
isupper(c); // true if uppercase
islower(c); // true if lowercase
toupper(c); // converts to uppercase
tolower(c); // converts to lowercase
Character Frequency Counting
hljs cpp
int freq[26] = {}; // for lowercase letters
for (char c : s) {
freq[c - 'a']++;
}
// Check if all characters are unique
bool unique = true;
for (int i = 0; i < 26; i++) {
if (freq[i] > 1) { unique = false; break; }
}
Palindrome Check
hljs cpp
bool isPalindrome(const string &s) {
int n = s.size();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - 1 - i]) return false;
}
return true;
}
Summary
std::stringsupports indexing,find,substr,push_back,insert,erase- Characters are ASCII integers — arithmetic on them is O(1)
c - '0'converts char to digit;d + '0'converts digit to char- Frequency arrays with
freq[c - 'a']are the fastest way to count characters - Always cast to
intwhen comparing string size with integers to avoid signed/unsigned warnings
Prefer reading offline? Download the full book as a PDF.
Download Free Book