Strings in C++



In this tutorial, we will learn about strings in C++. We will cover the basics of string manipulation, including creating, accessing, modifying, and performing operations on strings.


What is a String

A string in C++ is a sequence of characters. The C++ Standard Library provides the std::string class to represent and manipulate strings. Strings are used for storing and handling text data.


Creating Strings

Strings can be created in C++ using the std::string class:

#include <string>
std::string str = "Hello, world!";

Strings can also be initialized using character arrays:

char str[] = "Hello, world!";


Example 1: Initializing Strings

  1. Include the <string> header file.
  2. Create a string variable and initialize it with a value.
  3. Print the string variable.

C++ Program

#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "Hello, world!";
    cout << str;
    return 0;
}

Output

Hello, world!


Example 2: Accessing Characters in a String

  1. Include the <string> header file.
  2. Create a string variable and initialize it with a value.
  3. Access and print individual characters using the [] operator or the at() method.

C++ Program

#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "Hello";
    cout << str[0] << endl; // Accessing using []
    cout << str.at(1) << endl; // Accessing using at()
    return 0;
}

Output

H
e


Example 3: Modifying Strings

  1. Include the <string> header file.
  2. Create a string variable and initialize it with a value.
  3. Modify individual characters or append new characters using the += operator.
  4. Print the modified string.

C++ Program

#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "Hello";
    str[0] = 'J'; // Modifying individual character
    str += " World!"; // Appending new characters
    cout << str;
    return 0;
}

Output

Jello World!


Example 4: String Concatenation

  1. Include the <string> header file.
  2. Create two string variables and initialize them with values.
  3. Concatenate the strings using the + operator.
  4. Print the concatenated string.

C++ Program

#include <iostream>
#include <string>
using namespace std;
int main() {
    string str1 = "Hello";
    string str2 = " World!";
    string str3 = str1 + str2; // Concatenating strings
    cout << str3;
    return 0;
}

Output

Hello World!


Example 5: Finding Substrings

  1. Include the <string> header file.
  2. Create a string variable and initialize it with a value.
  3. Use the find() method to find a substring.
  4. Print the position of the found substring.

C++ Program

#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "Hello, world!";
    size_t pos = str.find("world"); // Finding substring
    if (pos != string::npos) {
        cout << "Found 'world' at position: " << pos;
    } else {
        cout << "Substring not found";
    }
    return 0;
}

Output

Found 'world' at position: 7


Example 6: String Length

  1. Include the <string> header file.
  2. Create a string variable and initialize it with a value.
  3. Use the length() or size() method to get the length of the string.
  4. Print the length of the string.

C++ Program

#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "Hello, world!";
    cout << "Length of the string: " << str.length(); // Getting string length
    return 0;
}

Output

Length of the string: 13