Strings Functions
Strings Functions
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
// Substring
cout << "Substring: " << str.substr(7, 5) << endl;
// Concatenation
string str2 = " Welcome!";
cout << "Concatenated string: " << str + str2 << endl;
// Searching
string searchStr = "World";
int found = str.find(searchStr);
if (found >=0) {
cout << "Found at index: " << found << endl;
} else {
cout << "Not found." << endl;
}
// Replacing
string replaceStr = "Universe";
str.replace(7, searchStr.length(), replaceStr);
cout << "Replaced string: " << str << endl;
// Character manipulation
for (char& c : str) {
if (islower(c)) {
c = toupper(c);
} else if (isupper(c)) {
c = tolower(c);
}
}
cout << "Case-reversed string: " << str << endl;
return 0;
}
/*
This program demonstrates some commonly used string functions: