Notes / C++ / Strings

Strings in C++ — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Strings Unlike C, C++ has a proper string type: std::string (from the <string> header).

Syntax

syntax
#include <string>
string name = "text";
name.length();
name + otherString;

Example

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "CodeyHub";
    cout << "Name: " << name << endl;
    cout << "Length: " << name.length() << endl;
    return 0;
}

Output

outputName: CodeyHub Length: 8

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview Be ready to contrast std::string with C's char arrays: automatic memory management vs manual, and == works as expected.
Back to notes Read full lesson →