Table of contents | |
Introduction | |
Creating and Initializing Strings | |
String Concatenation | |
String Length | |
String Comparison | |
String Substring | |
Sample Problems | |
Conclusion |
When working with text and manipulating strings in C++, the string class is a powerful tool that provides a wide range of functionalities. It is part of the Standard Template Library (STL) and simplifies string handling, making it easier to work with and manipulate text data. In this article, we will explore the string class in C++ and its various applications.
To start using the string class, we first need to include the '<string>' header in our code. Here's an example of how to create and initialize a string variable:
#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello, world!";
std::cout << greeting << std::endl;
return 0;
}
Hello, world!
In the above code, we include the necessary headers and declare a string variable called 'greeting'. We initialize it with the text "Hello, world!" and then output the string using 'std::cout'.
One of the most common operations with strings is concatenation, which involves combining two or more strings into a single string. In C++, we can use the '+' operator or the '+=' operator to concatenate strings. Let's see an example:
#include <iostream>
#include <string>
int main() {
std::string firstName = "John";
std::string lastName = "Doe";
std::string fullName = firstName + " " + lastName;
std::cout << "Full Name: " << fullName << std::endl;
return 0;
}
Full Name: John Doe
In the above code, we declare three string variables: 'firstName', 'lastName', and 'fullName'. We concatenate the first name, a space, and the last name using the '+' operator and assign the result to the 'fullName' variable.
To determine the length of a string, we can use the 'length()' or 'size()' member functions of the string class. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string message = "Hello, world!";
int length = message.length();
std::cout << "Length: " << length << std::endl;
return 0;
}
Length: 13
In the above code, we declare a string variable 'message' and assign it the value "Hello, world!". We then use the 'length()' function to get the length of the string and store it in the 'length' variable.
To compare two strings in C++, we can use the '==' operator, which returns 'true' if the strings are equal and 'false' otherwise. Let's see an example:
#include <iostream>
#include <string>
int main() {
std::string password = "password123";
std::string input;
std::cout << "Enter the password: ";
std::cin >> input;
if (input == password) {
std::cout << "Access granted!" << std::endl;
} else {
std::cout << "Access denied!" << std::endl;
}
return 0;
}
In the above code, we declare a string variable 'password' and initialize it with the value "password123". We then prompt the user to enter a password and store it in the input variable. Using the '==' operator, we compare the user input with the password and display the appropriate message.
Sometimes, we need to extract a portion of a string, known as a substring. The string class provides a 'substr()' member function that allows us to do just that. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string sentence = "The quick brown fox";
std::string substring = sentence.substr(4, 5);
std::cout << "Substring: " << substring << std::endl;
return 0;
}
Substring: quick
In the above code, we declare a string variable 'sentence' and assign it the value "The quick brown fox". We then use the 'substr()' function to extract a substring starting at index 4 and consisting of 5 characters.
1. Write a program that takes a string as input and counts the number of vowels (a, e, i, o, u) in the string.
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::cin >> input;
int vowelCount = 0;
std::string vowels = "aeiou";
for (char c : input) {
if (vowels.find(tolower(c)) != std::string::npos) {
vowelCount++;
}
}
std::cout << "Number of vowels: " << vowelCount << std::endl;
return 0;
}
2. Write a program that reverses a given string and prints the reversed string.
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::cin >> input;
std::string reversed = "";
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input[i];
}
std::cout << "Reversed string: " << reversed << std::endl;
return 0;
}
In this article, we have explored the string class in C++ and its various applications. We have seen how to create and initialize strings, perform concatenation, determine the length of a string, compare strings, and extract substrings. The string class provides a rich set of functions and makes working with text data in C++ much easier. By understanding and utilizing these string operations, you can efficiently handle and manipulate strings in your C++ programs.
70 videos|45 docs|15 tests
|
|
Explore Courses for EmSAT Achieve exam
|