In C++, a string is a sequence of characters represented as an array of characters. Strings are used extensively in programming for tasks like storing text, processing input/output, and manipulating data. This chapter will cover various string manipulation techniques in C++, starting from the basics and progressing to more advanced topics.
Strings in C++ are handled using the standard library std::string
class. Here are some fundamental operations:
In C++, you can declare and initialize strings using the std::string
class.
#include
#include
int main() {
// Declaration and initialization
std::string str = "Hello, World!";
std::cout << str << std::endl;
return 0;
}
// output //
Hello, World!
<iostream>
and <string>
for input/output operations and string handling respectively.str
and initialize it with the value "Hello, World!"
.std::cout
.You can concatenate two strings using the +
operator.
#include
#include
int main() {
std::string str1 = "Hello, ";
std::string str2 = "World!";
std::string result = str1 + str2;
std::cout << result << std::endl;
return 0;
}
// output //
Hello, World!
str1
and str2
and initialize them with "Hello, "
and "World!"
respectively.+
operator and store the result in another string variable result
.You can get the length of a string using the length()
function.
#include
#include
int main() {
std::string str = "Hello";
std::cout << "Length of string: " << str.length() << std::endl;
return 0;
}
// output //
Length of string: 5
str
and initialize it with the value "Hello"
.length()
function to get the length of the string and print it using std::cout
.You can read a string from the user using std::getline()
.
#include
#include
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
// output //
Enter your name: John
Hello, John!
name
to store the user’s name.std::cout
.std::getline()
function which reads the entire line of input including spaces.std::cout
.You can extract substrings from a string using the substr()
function.
#include
#include
int main() {
std::string str = "Hello, World!";
std::string substr = str.substr(7, 5); // Extracts "World"
std::cout << substr << std::endl;
return 0;
}
// output //
World
str
and initialize it with the value "Hello, World!"
.substr()
function to extract a substring starting from index 7 and of length 5."World"
is stored in the variable substr
, which we then print.You can search for substrings within a string using the find()
function.
#include
#include
int main() {
std::string str = "The quick brown fox jumps over the lazy dog.";
size_t found = str.find("fox");
if (found != std::string::npos)
std::cout << "Substring found at index " << found << std::endl;
else
std::cout << "Substring not found!" << std::endl;
return 0;
}
// output //
Substring found at index 16
str
and initialize it with a sentence.find()
function to search for the substring "fox"
within the string.You can compare two strings using the compare()
function.
#include
#include
int main() {
std::string str1 = "apple";
std::string str2 = "banana";
if (str1.compare(str2) == 0)
std::cout << "Strings are equal" << std::endl;
else if (str1.compare(str2) < 0)
std::cout << "String 1 is less than String 2" << std::endl;
else
std::cout << "String 1 is greater than String 2" << std::endl;
return 0;
}
// output //
String 1 is less than String 2
str1
and str2
and initialize them with "apple"
and "banana"
respectively.compare()
function to compare the two strings. If the result is 0, the strings are equal. If the result is negative, str1
is less than str2
. If the result is positive, str1
is greater than str2
.You can insert a substring into a string using the insert()
function.
#include
#include
int main() {
std::string str = "Hello!";
str.insert(5, " World");
std::cout << str << std::endl;
return 0;
}
// output //
Hello World!
str
and initialize it with the value "Hello!"
.insert()
function to insert the substring " World"
at index 5."Hello World!"
, which we then print.You can erase a portion of a string using the erase()
function.
#include
#include
int main() {
std::string str = "This is a sentence.";
str.erase(5, 3); // Erase "is "
std::cout << str << std::endl;
return 0;
}
// output //
This a sentence.
str
and initialize it with the value "This is a sentence."
.erase()
function to remove 3 characters starting from index 5."This a sentence."
, which we then print.we covered various aspects of string manipulation in C++. From basic operations like concatenation and length calculation to more advanced techniques such as substring extraction and string searching, you now have a comprehensive understanding of how to work with strings effectively in C++. Strings are a vital component of programming, and mastering their manipulation will greatly enhance your skills as a C++ developer. Experiment with the examples provided and explore further to deepen your understanding. Happy coding!Happy coding !❤️