In this tutorial, you'll learn how to manipulate strings in C++. C++ provides a variety of built-in functions that allow us to process and format strings effectively.
By the end of this tutorial, you'll be able to:
Prerequisites: Basic knowledge of C++ is required. Familiarity with data types, particularly strings, would be beneficial.
C++ strings are stored as a collection of characters. This allows us to perform various operations on them, like concatenation, comparison, and more.
Let's dive into some of the common string manipulation techniques.
Concatenation in C++ is done using the +
operator.
string str1 = "Hello";
string str2 = "World";
string str3 = str1 + " " + str2; // Concatenation
cout << str3; // Outputs: Hello World
C++ allows us to compare strings using the ==
(equality) and !=
(inequality) operators.
string str1 = "ABC";
string str2 = "ABC";
if (str1 == str2) {
cout << "Strings are equal."; // This will be printed
} else {
cout << "Strings are not equal.";
}
We can extract a substring from a string using the substr()
function. It takes two parameters: the starting index and the length of the substring.
string str = "Hello, World!";
string sub = str.substr(7, 5); // Get substring starting from index 7 and of length 5
cout << sub; // Outputs: World
Remember, string indices start from 0 in C++.
Let's look at some practical examples.
This example shows how to find the length of a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
cout << "Length of string is " << str.length(); // Outputs: Length of string is 13
return 0;
}
This example shows how to replace a part of the string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
str.replace(7, 5, "C++"); // Replace 'World' with 'C++'
cout << str; // Outputs: Hello, C++!
return 0;
}
In this tutorial, we've covered basic string manipulation techniques in C++, including concatenation, comparison, and substring extraction. Practice these techniques to get a better grip on them.
As a next step, you can explore more advanced string manipulation techniques and built-in functions in C++. You can refer to the C++ documentation for more details.
Here are the solutions for the exercises:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2;
cout << "Enter first string: ";
cin >> str1;
cout << "Enter second string: ";
cin >> str2;
string result = str1 + str2;
cout << "Result: " << result;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2;
cout << "Enter first string: ";
cin >> str1;
cout << "Enter second string: ";
cin >> str2;
if (str1 == str2) {
cout << "Strings are equal.";
} else {
cout << "Strings are not equal.";
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int start, length;
cout << "Enter a string: ";
cin >> str;
cout << "Enter starting index and length for substring: ";
cin >> start >> length;
string sub = str.substr(start, length);
cout << "Substring: " << sub;
return 0;
}
Remember to keep practicing and experimenting with different scenarios. Happy coding!