C++ Academy · Lesson

String Algorithms

Search and split efficiently.

Lesson 4 of 413 steps

String Algorithms is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

Searching with find

find returns the index of a substring or npos if absent, working directly on a view without copying.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "key=value";
    auto pos = sv.find('=');
    std::cout << pos << "\n"; // 3
}

Checking npos

Always compare a search result against std::string_view::npos before using it as an index.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "abc";
    if (sv.find('z') == std::string_view::npos)
        std::cout << "not found\n";
}

Prefix and Suffix Checks

starts_with and ends_with (C++20) make intent obvious and avoid manual substring comparisons.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "report.pdf";
    std::cout << sv.starts_with("report") << "\n";
    std::cout << sv.ends_with(".pdf") << "\n";
}

Extracting a Field

Combine find with substr to split a key/value pair without allocating new strings.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "name=Ada";
    auto eq = sv.find('=');
    std::cout << sv.substr(0, eq) << " : " << sv.substr(eq + 1) << "\n";
}

Splitting by a Delimiter

You can walk through tokens by repeatedly finding the delimiter and taking subviews, each token is just a view.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "a,b,c";
    size_t start = 0, pos;
    while ((pos = sv.find(',', start)) != std::string_view::npos) {
        std::cout << sv.substr(start, pos - start) << "\n";
        start = pos + 1;
    }
    std::cout << sv.substr(start) << "\n";
}

Finding from the End

rfind searches backward, handy for getting a file extension or the last separator.

#include <iostream>
#include <string_view>
int main() {
    std::string_view path = "/usr/local/bin";
    auto slash = path.rfind('/');
    std::cout << path.substr(slash + 1) << "\n"; // bin
}

Trimming Whitespace

Use find_first_not_of and find_last_not_of with remove_prefix/remove_suffix to trim, all without copying.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "  hi  ";
    sv.remove_prefix(sv.find_first_not_of(" "));
    sv.remove_suffix(sv.size() - 1 - sv.find_last_not_of(" "));
    std::cout << "[" << sv << "]\n"; // [hi]
}

Comparing Views

Views support == and compare for lexicographic comparison, comparing characters, not pointers.

#include <iostream>
#include <string_view>
int main() {
    std::string_view a = "apple", b = "apple";
    std::cout << (a == b) << "\n"; // 1
}

Counting Occurrences

A loop with find advancing past each hit counts how many times a character or substring appears.

#include <iostream>
#include <string_view>
int main() {
    std::string_view sv = "mississippi";
    int count = 0;
    for (size_t p = sv.find('s'); p != std::string_view::npos; p = sv.find('s', p + 1)) ++count;
    std::cout << count << "\n"; // 4
}

Algorithms Stay Allocation-Free

All of these operations work on the same underlying buffer. Tokens and trimmed results are subviews, no heap allocation occurs until you build a std::string.

Mind the Lifetimes

Subviews produced by these algorithms share the original buffer's lifetime. Do not let them outlive the source string.

Quick Check

Check your understanding of string_view algorithms.

Recap

You learned string algorithms on views:

  • find/rfind return positions or npos.
  • starts_with/ends_with check prefixes and suffixes.
  • Split, trim, and tokenize produce allocation-free subviews.
  • Subviews share the source's lifetime, mind dangling.
Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “String Algorithms” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “String Algorithms”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “String Algorithms”?

Search and split efficiently. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “String Algorithms” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why string_view
  2. Creating Views
  3. Pitfalls and Lifetimes
  4. String Algorithms
← Back to C++ Academy