-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString Manipulation.cpp
More file actions
45 lines (36 loc) · 1.02 KB
/
String Manipulation.cpp
File metadata and controls
45 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "welcome";
// Concatenation
string greeting = str + ", Friend!";
cout << "Concatenation: " << greeting << endl;
// Substring
string part = greeting.substr(7, 5); // Extracts "World"
cout << "Substring: " << part << endl;
// Find
size_t pos = greeting.find("Friend");
cout << "Find: 'Friend' found at position " << pos << endl;
// Replace
string replaced = greeting;
replaced.replace(pos, 5, "C++");
cout << "Replace: " << replaced << endl;
// Insert
string inserted = greeting;
inserted.insert(5, " beautiful");
cout << "Insert: " << inserted << endl;
// Erase
string erased = greeting;
erased.erase(5, 7); // Removes " beautiful"
cout << "Erase: " << erased << endl;
// To uppercase
string uppercase = greeting;
for (char &c : uppercase) c = toupper(c);
cout << "To Uppercase: " << uppercase << endl;
// To lowercase
string lowercase = greeting;
for (char &c : lowercase) c = tolower(c);
cout << "To Lowercase: " << lowercase << endl;
return 0;
}