String in C++
In C++, strings are a sequence of characters or a collection of different characters represented using the std::string class. This class provides a convenient and flexible way to work with text data. To use strings in C++, you need to include the <string> header in your program.
Here are some common tasks and operations related to working with strings in C++:
- Creating and Initializing a String:
- Concatenating Strings:
- String Length:
- Accessing Individual Characters:
- String Comparison:
C++ follows the OOPs concept which allows representing the string as an object of the C++ String class (std:: string). The class allows us to declare a string variable quickly, and store any sequence of characters in it.
Here's an example that demonstrates the use of strings with the help of the string class in C++:
#include <iostream>
#include <string>
int main() {
std::string greet = "Hello, World!";
std::cout << greet << std::endl;
}
The output of the above code is as follows:
Hello, World!
You can concatenate strings using the + operator or the append() method. Here's an example:
#include <iostream>
#include <string>
int main() {
// Concatenating using + operator
std::string first_name = "John";
std::string last_name = "Xyz";
std::string full_name = first_name + " " + last_name;
std::cout << "Full Name: " << full_name << std::endl;
// Use the append method to concatenate the secondString to the end of the firstString
std::string first_string = "Hello, ";
std::string second_string = "World!";
std::string full_string = first_string.append(second_string);
std::cout << "Full String: " << full_string << std::endl;
return 0;
}
The output of the above code is as follows:
Full Name: John Xyz Full String: Hello, World!
In C++, you can find the length of a string using the length() or size() method:
#include <iostream>
#include <string>
int main() {
std::string first_name = "Danny";
std::string last_name = "Xyz";
std::cout << "First Name Length: " << first_name.size() << std::endl;
std::cout << "Last Name Length: " << last_name.size() << std::endl;
return 0;
}
The output of the above code is as follows:
First Name Length: 5 Last Name Length: 3
You can use array-like indexing to access characters of a string:
#include <iostream>
#include <string>
int main() {
std::string word = "Programming";
char first_char = word[0]; // Accessing the first character.
char last_char = word[word.length()-1]; // Accessing the last character.
std::cout << "First character: " << first_char << std::endl;
std::cout << "Last character: " << last_char << std::endl;
return 0;
}
The output of the above code is as follows:
First character: P Last character: g
In C++, you can compare two strings using the == operator. For example:
#include <iostream>
#include <string>
int main() {
std::string str1 = "mango";
std::string str2 = "apple";
if (str1 == str2) {
std::cout << "The strings are equal." << std::endl;
} else {
std::cout << "The strings are not equal." << std::endl;
}
return 0;
}
The output of the above code is as follows:
The strings are not equal.