100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Strings in C++

Learn how the C++ `std::string` class represents text, why it is safer and more convenient than C-style char arrays, and how to declare and use it.

StringsBeginner6 min readJul 7, 2026
Analogies

1. Intro

A string is a sequence of characters used to represent text, such as names, sentences, or file paths. C++ offers two ways to work with strings: the legacy C-style char array inherited from C, and the modern std::string class provided by the Standard Library. std::string is the recommended choice for almost all application code because it manages its own memory, resizes automatically, and comes with a rich set of built-in member functions.

🏏

Cricket analogy: Like choosing between an uncapped rookie who needs constant supervision (C-style char array) and a seasoned all-rounder like Ravindra Jadeja who manages his own game plan (std::string) - the experienced player handles ball changes and field shifts automatically.

2. Syntax

cpp
#include <string>
using namespace std;

string name = "Alex";          // direct initialization
string greeting("Hello");        // constructor style
string empty;                    // empty string, length 0
string copy = name;              // copy of another string

3. Explanation

To use std::string you must #include <string>. Internally, a string object stores its characters in dynamically allocated memory and automatically grows or shrinks as content changes, so you never need to worry about buffer size the way you do with C-style strings. std::string supports operators like +, +=, ==, < directly, can be read with cin >> or getline(), printed with cout <<, and offers member functions such as .length(), .substr(), .find(), and .append(). Because it is a proper class, passing, returning, and copying strings is safe and predictable.

🏏

Cricket analogy: Importing <string> is like a franchise signing Jasprit Bumrah before the IPL auction opens - you must bring in the toolkit (+, .substr(), .find()) before you can use its full range of deliveries in a match.

4. Example

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

int main() {
    string first = "Hello";
    string second = "World";
    string sentence = first + ", " + second + "!";

    cout << sentence << endl;
    cout << "Length: " << sentence.length() << endl;
    return 0;
}

5. Output

text
Hello, World!
Length: 13

6. Key Takeaways

  • std::string is defined in <string> and represents text as a dynamically sized, growable object.
  • It supports natural operators (+, +=, ==, <) and dozens of member functions like .length() and .substr().
  • Unlike C-style strings, std::string manages its own memory, so there is no manual buffer-size management.
  • std::string is the default, recommended way to handle text in modern C++ code.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#StringsInC#Strings#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep