Dataslope logoDataslope

Strings

Work with std::string, C-strings, string_view, searching, slicing, splitting, reversing, and palindrome checks in C++

std::string is the C++ text container you will use constantly. Think of it as a dynamic array of characters with extra operations for concatenation, search, comparison, and slicing.

Creating and reading strings

A string owns its characters and tracks its length. Like std::vector, indexing is O(1), and the string can grow as needed.

Code Block
C++ 20 (202002L)

Use .size() or .length(); they mean the same thing for std::string.

Iterating over characters

Characters are values of type char. You can iterate by index when you need positions, or by range-for when you only need each character.

Code Block
C++ 20 (202002L)

Common operations

find returns the index of a match, or std::string::npos when no match exists. substr(pos, count) copies a substring. replace modifies a range.

Code Block
C++ 20 (202002L)

Concatenation

+ creates a new string. += appends to an existing string and is often better inside loops.

Code Block
C++ 20 (202002L)

C-strings and conversion

A C-string is a null-terminated character array usually represented as const char*. C++ string literals can initialize std::string, and .c_str() gives a pointer view for APIs that need C-style strings.

Code Block
C++ 20 (202002L)

Do not store dangling C-string pointers

A pointer returned by .c_str() is only valid while the string object is alive and not modified in a way that reallocates its storage.

std::string_view

std::string_view is a non-owning view into characters. It is great for read-only function parameters when you want to accept std::string, string literals, and slices without copying.

Code Block
C++ 20 (202002L)

Splitting by a delimiter

A simple split helper repeatedly calls find and substr. This is a useful pattern for parsing CSV-like toy inputs and interview strings.

Code Block
C++ 20 (202002L)

Reversing and palindrome checks

A two-pointer scan can compare the first and last characters, then move inward.

Code Block
C++ 20 (202002L)

String is dynamic

Like std::vector<char>, std::string can reallocate when it grows. Prefer appending with += when building text gradually.

Practice: palindrome function

Challenge
C++ 20 (202002L)
isPalindrome(const std::string& s)

Implement isPalindrome using a two-pointer scan. The driver prints true or false for a hardcoded test string.

Practice: count vowels

Challenge
C++ 20 (202002L)
countVowels(const std::string& s)

Implement countVowels for lowercase vowels a/e/i/o/u. The driver prints the count for a hardcoded string.

Check your understanding

QuestionSelect one

What is the time complexity of indexing a valid position in a std::string with s[i]?

O(1)

O(log n)

O(n)

O(n log n)

QuestionSelect one

Why might a function take std::string_view instead of const std::string&?

It can accept strings, literals, and slices without owning or copying the characters.

It always extends the lifetime of temporary character data.

It allows mutation of the original string.

It stores characters more compactly than char.

QuestionSelect one

What does s.substr(pos, count) return for a std::string s?

A pointer into the original string that can mutate it.

A new string containing up to count characters starting at pos.

The number of characters after pos.

A boolean indicating whether the substring exists.

On this page