String Manipulation Techniques in Cpp
Case Conversion (Upper/Lowercase)
In C++, you can convert strings to uppercase or lowercase using functions provided by the
Converting to Uppercase:
#include <cctype>
#include <algorithm>
std::string str = "Hello World";
// Using toupper() function from <cctype> header
for (char& c: str) {
c = std::toupper(c);
}
// Using std::transform() algorithm
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::toupper(c);
});
Converting to Lowercase:
#include <cctype>
#include <algorithm>
std::string str = "Hello World";
// Using tolower() function from <cctype> header
for (char& c : str) {
c = std::tolower(c);
}
// Using std::transform() algorithm
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::tolower(c);
});
Removing Whitespace
To remove whitespace (spaces, tabs, newlines) from a string in C++, you can use various techniques, such as using the std::remove_if() algorithm with a predicate or utilizing the erase-remove idiom. Examples:
Removing Whitespace using std::remove_if():
#include <algorithm>
#include <cctype>
std::string str = " Hello World ";
str.erase(std::remove_if (str.begin(), str.end, [](unsigned char c) {
return std::isspace(c);
}), str.end());
Removing Whitespace using the erase-remove idiom:
std::string str = " Hello World ";
str.erase(std::remove(str.begin(), str.end(), '), str.end());
Splitting Strings
To split a string into multiple substrings based on a delimiter in C++, you can use functions like std::getline(), std::istringstream, or utilize the std::regex library. Examples:
Splitting a string using std::getline():
#include <sstream>
#include <vector>
std::string str = "Hello, World, How, Are, You";
std::vector<std::string> tokens;
std::istringstream iss(str);
std::string token;
while (std::getline(iss, token, ',')) {
tokens.push_back(token);
}
Splitting a string using std::istringstream:
#include <sstream>
#include <vector>
std::string str = "Hello World How Are You";
std::vector<std::string> tokens;
std::istringstream iss(str);
std::string token;
while (iss >> token) {
tokens.push_back(token);
}
Joining Strings
To join multiple strings together in C++, you can use the + operator, std::stringstream, or the std::accumulate() algorithm. Examples:
Joining strings using the + operator:
#include <vector>
#include <string>
std::vector<std::string> words = {"Hello", "World", "How", "Are", "You"};
std::string joinedString;
for (const std::string& word: words) {
joinedString += word;
}
// Result: "HelloWorldHowAreYou"
Joining strings using std::stringstream:
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> words = {"Hello", "World", "How", "Aze", "You"};
std::stringstream ss;
for (const std::string& word: words) {
ss << word;
}
std::string joinedString = ss.str();
// Result: "HelloWorldHowAreYou"
Note : The std::accumulate() algorithm can also be used to join strings by providing a custom binary operation, but it requires the