Strings in C++ (original) (raw)

Last Updated : 26 Apr, 2026

Strings in C++ are objects of the std::string class. They are used to represent and manipulate sequences of characters.

#include #include using namespace std;

int main() {

// Creating a string
string str = "Hello Geeks";

// Traversing using index
cout << "Using index: ";
for (int i = 0; i < str.size(); i++) {
    cout << str[i] ;
}
cout << endl;

// Traversing using range-based for loop
cout << "Using range-based for loop: ";
for (char ch : str) {
    cout << ch ;
}
cout << endl;

// Traversing using iterator
cout << "Using iterator: ";
for (auto it = str.begin(); it != str.end(); it++) {
    cout << *it ;
}
cout << endl;

return 0;

}

`

Output

Using index: Hello Geeks Using range-based for loop: Hello Geeks Using iterator: Hello Geeks

Syntax

The string container is defined as std::string class inside the header file.

string str;

where,

Basic Operations in String

Basic operations on Strings are shown below:

Initializing a String

#include #include using namespace std;

int main() {

// Initializing a string directly
string str = "Hello Geeks";

// Printing the string
cout << str << endl;

return 0;

}

`

Accessing Characters

#include #include using namespace std;

int main() { string str = "Hello Geeks";

// Access using index operator []
cout << "First character: " << str[0] << endl;
cout << "Fifth character: " << str[4] << endl;

// Access using at()
cout << "Character at index 6: " << str.at(6) << endl;

return 0;

}

`

Output

First character: H Fifth character: o Character at index 6: G

String Length

#include #include using namespace std;

int main() { string str = "Hello Geeks";

// Using size()
cout << "Length using size(): " << str.size() << endl;

// Using length()
cout << "Length using length(): " << str.length() << endl;

return 0;

}

`

Concatenation of Strings

#include #include using namespace std;

int main() { string str1 = "Hello"; string str2 = " Geeks";

// Using + operator
string result1 = str1 + str2;
cout << "Concatenation using + : " << result1 << endl;

// Using append() function
string result2 = str1;
result2.append(str2);
cout << "Concatenation using append(): " << result2 << endl;

return 0;

}

`

Output

Concatenation using + : Hello Geeks Concatenation using append(): Hello Geeks

Modifying a String

#include #include using namespace std;

int main() { string str = "Hello Geeks";

// Adding a character at the end
str.push_back('!');
cout << "After push_back: " << str << endl;

// Removing the last character
str.pop_back();
cout << "After pop_back: " << str << endl;

// Inserting a substring
str.insert(5, " C++");
cout << "After insert: " << str << endl;

// Erasing part of the string
str.erase(5, 4); 
cout << "After erase: " << str << endl;

return 0;

}

`

Output

After push_back: Hello Geeks! After pop_back: Hello Geeks After insert: Hello C++ Geeks After erase: Hello Geeks

Substring Extraction

#include #include using namespace std;

int main() { string str = "Hello Geeks";

// Extract "Hello"
string sub1 = str.substr(0, 5);   
cout << "Substring 1: " << sub1 << endl;

// Extract "Geeks"
string sub2 = str.substr(6, 5);  
cout << "Substring 2: " << sub2 << endl;

return 0;

}

`

Output

Substring 1: Hello Substring 2: Geeks

Know more about Substring extraction in C++.

Searching in a String

#include #include using namespace std;

int main() { string str = "Hello Geeks";

size_t pos = str.find("Geeks");  

if (pos < str.size()) {
    cout << "\"Geeks\" found at index: " << pos << endl;
}

return 0;

}

`

Output

"Geeks" found at index: 6

Learn more about find() in C++

C++ String Functions

C++ provides some inbuilt functions which are used for string manipulation, such as the strcpy() and strcat() functions for copying and concatenating strings. Some of them are:

Function Description
length() This function returns the length of the string.
swap() This function is used to swap the values of 2 strings.
size() Used to find the size of string
resize() This function is used to resize the length of the string up to the given number of characters.
push_back() This function is used to push the passed character at the end of the string
pop_back() This function is used to pop the last character from the string
clear() This function is used to remove all the elements of the string.
find() This function is used to search for a certain substring inside a string and returns the position of the first character of the substring.
replace() Replaces a portion of the string with another string.
substr() This function is used to create a substring from a given string.
compare() This function is used to compare two strings and returns the result in the form of an integer.
erase() This function is used to remove a certain part of a string.
rfind() This function is used to find the string's last occurrence.

These functions are discussed in this article in more detail - String Function in C++