#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
// Length of the string
cout << "Length: " << str.length() << endl;
// Accessing individual characters
cout << "First character: " << str[0] << endl;
cout << "Last character: " << str[str.length() - 1] << endl;
// 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:
length(): Retrieves the length of the string.
operator[]: Accesses individual characters of the string.
substr(): Extracts a substring from the string.
operator+: Concatenates two strings.
find(): Searches for a substring within the string.
replace(): Replaces a substring with another string.
islower(), toupper(), isupper(), tolower(): Manipulates the case of characters in
the string.*/