A string is a sequence of characters used to store text such as
names, addresses, messages, sentences, and other textual information.
In modern C++, the std::string class is commonly used for
working with strings.
std::string, include the
<string> header file.
A string is a sequence of characters. For example:
"Hello"
"Rahul"
"Welcome to C++"
Strings are used whenever a program needs to work with text.
The std::string class is provided by the
<string> header.
#include <iostream>
#include <string>
After including the header, we can create string variables.
std::string name;
Here, name is a string variable that can store text.
Example:
std::string city = "Aurangabad";
A string can be initialized when it is declared.
std::string name = "Amit";
The string "Amit" is stored in the variable
name.
#include <iostream>
#include <string>
int main() {
std::string name = "Rahul";
std::cout << name;
return 0;
}
Output:
Rahul
The >> operator can be used to read a string from the
user. It reads input up to whitespace.
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello "
<< name;
If the user enters Rahul, the program stores that word in
the string.
Use std::getline() when you want to read a complete line,
including spaces.
std::string fullName;
std::cout << "Enter your full name: ";
std::getline(std::cin, fullName);
std::cout << fullName;
For example, Rahul Kumar can be read as one complete
string.
std::string message =
"Welcome to C++ Programming";
std::cout << message;
A std::string can contain spaces and multiple words.
The length() function returns the number of characters in
a string.
std::string name = "Rahul";
std::cout << name.length();
Output:
5
The size() function can also be used to get the number of
characters in a string.
std::string word = "Computer";
std::cout << word.size();
Output:
8
For a std::string, size() and
length() provide the same count of characters.
Individual characters can be accessed using an index.
std::string word = "Hello";
std::cout << word[0];
Output:
H
Like arrays, string indexes start from 0.
std::string word = "Hello";
std::cout << word[word.length() - 1];
Output:
o
The last character is located at index
length() - 1.
Characters in a non-const std::string can be changed using
an index.
std::string word = "Hello";
word[0] = 'Y';
std::cout << word;
Output:
Yello
A for loop can be used to process each character.
std::string word = "Hello";
for (int i = 0; i < word.length(); i++) {
std::cout << word[i] << std::endl;
}
Output:
H
e
l
l
o
The + operator can be used to concatenate strings.
std::string firstName = "Rahul";
std::string lastName = "Kumar";
std::string fullName =
firstName + " " + lastName;
std::cout << fullName;
Output:
Rahul Kumar
The += operator can add text to the end of a string.
std::string message = "Hello";
message += " World";
std::cout << message;
Output:
Hello World
Strings can be compared using operators such as == and
!=.
std::string password = "admin";
if (password == "admin") {
std::cout << "Correct password";
}
The == operator checks whether two strings have the same
content.
std::string city1 = "Patna";
std::string city2 = "Delhi";
if (city1 != city2) {
std::cout << "Cities are different";
}
The != operator checks whether the strings are different.
The find() function can be used to search for a character
or substring.
std::string word = "Computer";
std::size_t position = word.find('p');
if (position != std::string::npos) {
std::cout << "Character found";
}
If the item is not found, find() returns
std::string::npos.
std::string sentence =
"I am learning C++";
std::size_t position =
sentence.find("C++");
if (position != std::string::npos) {
std::cout << "C++ found";
}
The find() function can search for a sequence of characters
inside a string.
The substr() function extracts part of a string.
std::string word = "Programming";
std::string part =
word.substr(0, 7);
std::cout << part;
Output:
Program
The first argument is the starting position and the second specifies the number of characters.
The erase() function can remove characters from a string.
std::string word = "Hello World";
word.erase(5, 6);
std::cout << word;
Output:
Hello
The first argument specifies the starting position and the second specifies how many characters to remove.
The insert() function inserts characters into a string.
std::string word = "Hello World";
word.insert(6, "C++ ");
std::cout << word;
Output:
Hello C++ World
The replace() function can replace part of a string.
std::string text =
"I like Java";
text.replace(7, 4, "C++");
std::cout << text;
Output:
I like C++
The clear() function removes all characters from a
string.
std::string message = "Hello World";
message.clear();
std::cout << message;
After clear(), the string is empty.
The empty() function checks whether a string contains no
characters.
std::string name;
if (name.empty()) {
std::cout << "String is empty";
}
It returns true when the string has no characters.
#include <iostream>
#include <string>
int main() {
std::string firstName;
std::string lastName;
std::cout << "Enter first name: ";
std::cin >> firstName;
std::cout << "Enter last name: ";
std::cin >> lastName;
std::string fullName =
firstName + " " + lastName;
std::cout << "Full Name: "
<< fullName;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string sentence;
std::cout << "Enter a sentence: ";
std::getline(std::cin, sentence);
std::cout << "You entered: "
<< sentence << std::endl;
std::cout << "Length: "
<< sentence.length();
return 0;
}
std::getline() is useful when the input may contain spaces.
std::string for normal text processing.<string> header when using
std::string.std::getline() when spaces need to be included in input.find() for searching text.empty() to check whether a string has no characters.| Concept | Meaning |
|---|---|
| std::string | Common C++ type for storing text. |
| length() | Returns the number of characters in a string. |
| size() | Also returns the number of characters in a string. |
| getline() | Reads a complete line, including spaces. |
| find() | Searches for a character or substring. |
| substr() | Extracts part of a string. |
| erase() | Removes characters from a string. |
| insert() | Inserts text into a string. |
| replace() | Replaces part of a string. |
| clear() | Removes all characters from a string. |
#include <iostream>
#include <string>
int main() {
std::string message =
"Hello C++";
std::cout << message;
return 0;
}
std::string is commonly used for text in modern C++.<string> header is used with std::string.length() and size() return the number of characters.std::getline() can read a complete line containing spaces.+ operator can concatenate strings.find() searches for characters or substrings.substr(), erase(), insert(), and
replace() can modify or extract string content.clear() removes all characters from a string.Question: Which function is commonly used to read a complete line of text, including spaces?