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
- Include the
<string>
header file. - Create a string variable and initialize it with a value.
- 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
- Include the
<string>
header file. - Create a string variable and initialize it with a value.
- Access and print individual characters using the
[]
operator or theat()
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
- Include the
<string>
header file. - Create a string variable and initialize it with a value.
- Modify individual characters or append new characters using the
+=
operator. - 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
- Include the
<string>
header file. - Create two string variables and initialize them with values.
- Concatenate the strings using the
+
operator. - 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
- Include the
<string>
header file. - Create a string variable and initialize it with a value.
- Use the
find()
method to find a substring. - 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
- Include the
<string>
header file. - Create a string variable and initialize it with a value.
- Use the
length()
orsize()
method to get the length of the string. - 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