FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses C++ and STL Essentials

CSE

Progress0 / 6 lessons
  1. 1. What C++ adds over C
  2. 2. References vs pointers
  3. 3. vector and why it beats raw arrays
  4. 4. map and set with real complexity numbers
  5. 5. Sorting with comparators
  6. 6. Strings and stringstream

Courses › C++ and STL Essentials

Strings and stringstream

std::string operations, safe input, splitting text and converting numbers.

11 min read · Lesson 6 of 6 · Pro

std::string is a vector of characters that knows its length

Everything painful about C strings — the null terminator, strcpy overflows, sizing the array — is handled for you.

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "first";
    string b = "hack";

    string c = a + b;              // concatenation
    c += "!";                      // append

    cout << c << "\n";             // firsthack!
    cout << c.length() << "\n";    // 10  (size() is identical)
    cout << c[0] << c.back() << "\n";  // f!
The rest of this lesson is Pro

The free lessons of C++ and STL Essentials finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.