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.
Syntax
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
Reference
Core Ideas
Ideas every C++ string tutorial builds on:
Idea
Example
Remember
std::string
std::string s = "Hi";
Include <string>
Concatenation
a + " " + b or +=
Creates / updates a std::string
Length
s.length() / s.size()
Same count for typical text
Search
s.find("x")
Returns index or npos if missing
Equality
a == b
Compares content for std::string
C-string helpers
strlen, strcpy
Need <cstring> and a buffer
Code
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;
}
Output
Language: C++
Version: 17
Ready to learn strings!
How It Works
#include <string> brings in std::string.
language stores a string literal.
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;
}
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.