C++ Strings

Beginner
8 min read
Updated: Nov 2026
5 examples
std::string

Definition and Usage

In C++, a string is text: a sequence of characters. Modern C++ tutorials start with std::string from <string>—it grows automatically, supports + concatenation, and provides members like length() and find().

You will also meet C-style strings: null-terminated char arrays handled by <cstring> helpers such as strlen() and strcmp(). Prefer std::string for new code; learn both for interviews and C APIs.

This page teaches what a C++ string is. The String Functions index lists every function guide in this series.

Prefer std::string.

It manages memory for you. Use char arrays and <cstring> when a C API or fixed buffer requires it.

Two headers.

#include <string> for std::string. #include <cstring> for C helpers like strcpy and strlen.

C++ Syntax

Common ways to create and work with std::string:

C++
#include <iostream>
#include <string>

std::string literal = "Hello, C++!";
std::string full    = first + " " + last;  // + concatenation
std::size_t len     = text.length();       // or text.size()
std::size_t pos     = text.find("C++");    // search
bool same           = (a == b);            // content equality

Core Ideas

Ideas every C++ string tutorial builds on:

IdeaExampleRemember
std::stringstd::string s = "Hi";Include <string>
Concatenationa + " " + b or +=Creates / updates a std::string
Lengths.length() / s.size()Same count for typical text
Searchs.find("x")Returns index or npos if missing
Equalitya == bCompares content for std::string
C-string helpersstrlen, strcpyNeed <cstring> and a buffer

C++ Examples

Five short programs that define how strings work in a C++ tutorial.

Example 1 — Create and print

Assign a std::string and print it with std::cout.

C++
#include <iostream>
#include <string>

int main() {
    std::string language = "C++";
    int version = 17;

    std::cout << "Language: " << language << "\n";
    std::cout << "Version: " << version << "\n";
    std::cout << "Ready to learn strings!\n";
    return 0;
}

How It Works

  1. #include <string> brings in std::string.
  2. language stores a string literal.
  3. cout prints text with <<.

Example 2 — Join with + and +=

Build a message from smaller pieces.

C++
#include <iostream>
#include <string>

int main() {
    std::string first = "Hello";
    std::string last  = "C++";

    std::string greeting = first + ", " + last + "!";
    std::cout << greeting << "\n";

    std::string status = "Status: ";
    status += "Active";
    std::cout << status << "\n";
    return 0;
}

How It Works

  1. + joins std::string values (and string literals).
  2. += appends onto the same object.
  3. No manual buffer size is needed with std::string.

Example 3 — length() and indexing

Measure size and read a character by index.

C++
#include <iostream>
#include <string>

int main() {
    std::string word = "Hello";

    std::cout << "length: " << word.length() << "\n";
    std::cout << "first: " << word[0] << "\n";
    std::cout << "last: " << word[word.length() - 1] << "\n";
    return 0;
}

How It Works

  1. length() (or size()) returns the character count.
  2. [0] is the first character (zero-based).
  3. Bounds matter—do not index past length() - 1.

Example 4 — find()

Search for a substring inside a std::string.

C++
#include <iostream>
#include <string>

int main() {
    std::string url = "https://codetofun.com/cpp/string";
    std::size_t pos = url.find("/cpp");

    if (pos != std::string::npos) {
        std::cout << "found at: " << pos << "\n";
    } else {
        std::cout << "not found\n";
    }
    return 0;
}
find() Tutorial

How It Works

  1. find returns the start index of the match.
  2. npos means “not found.”
  3. Always compare with npos before using the index.

Example 5 — strlen() and strcmp()

C-style helpers on null-terminated char arrays.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* a = "Hello";
    const char* b = "Hello";

    std::cout << "strlen: " << std::strlen(a) << "\n";
    if (std::strcmp(a, b) == 0) {
        std::cout << "strcmp: equal\n";
    }
    return 0;
}
strlen() Tutorial

How It Works

  1. strlen counts until '\0'.
  2. strcmp returns 0 when strings match.
  3. Check == 0—do not treat the return as a boolean alone.

Next: C++ String Functions Index

After you know what a string is, open the function index for full tutorials on find() and the essential <cstring> helpers in this series.

Start withWhy
find()Search inside std::string
strlen()Length of a C-string
strcmp()Compare two C-strings
strcpy()Copy into a char buffer

Full list: C++ String Functions index (/cpp/string/function).

Frequently Asked Questions

A string is a sequence of characters. In modern C++ you usually use std::string from <string>. C-style strings are null-terminated char arrays handled with <cstring> helpers such as strlen() and strcmp().
Prefer std::string for new code—it manages memory and supports + and find(). Use char arrays and <cstring> for C APIs, fixed buffers, or legacy code. Interviews often test both.
With std::string use + or += : std::string full = first + " " + last;. For C-strings use strcat() or strncat() and keep buffers large enough.
text.length() (or size()) is a std::string member. strlen(ptr) counts characters in a null-terminated char array until '\0'. Prefer length() on std::string.
Practice the examples on this page, then open the String Functions index for find(), strlen(), strcpy(), strcmp(), and every other function guide in this C++ tutorial series.

Browse every string function

Open the functions index for categorized guides with examples and FAQs.

String Functions Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful