C++ provides the std::string
class to handle text-based data efficiently.
1️⃣ Basic Example of String in C++
📌 Declaring and displaying a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, C++!";
cout << str;
return 0;
}
🔹 Output
Hello, C++!
2️⃣ Addition using String
📌 Concatenating strings using +
operator.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = " World";
string result = str1 + str2;
cout << "Concatenated String: " << result;
return 0;
}
🔹 Output
Concatenated String: Hello World
3️⃣ String Append in C++
📌 Appending a string using .append()
.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
str.append(" C++");
cout << str;
return 0;
}
🔹 Output
Hello C++
4️⃣ String Capacity Function in C++
📌 Checking the capacity of a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
cout << "Length: " << str.length() << endl;
cout << "Capacity: " << str.capacity() << endl;
cout << "Max Size: " << str.max_size() << endl;
return 0;
}
🔹 Output
Length: 5
Capacity: 15
Max Size: 1073741823
5️⃣ String Character Change in C++
📌 Modifying a character in a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
str[0] = 'M';
cout << str;
return 0;
}
🔹 Output
Mello
6️⃣ String Concatenation in C++
📌 Using +=
operator for concatenation.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
str += " World";
cout << str;
return 0;
}
🔹 Output
Hello World
7️⃣ String Input Getline in C++
📌 Taking full-line input using getline()
.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a sentence: ";
getline(cin, str);
cout << "You entered: " << str;
return 0;
}
🔹 Output
Enter a sentence: Hello C++ World
You entered: Hello C++ World
8️⃣ String Length in C++
📌 Finding the length of a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "C++ Programming";
cout << "String Length: " << str.length();
return 0;
}
🔹 Output
String Length: 16
9️⃣ String Palindrome in C++
📌 Checking if a string is a palindrome.
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string str) {
string rev = string(str.rbegin(), str.rend());
return str == rev;
}
int main() {
string str = "madam";
if (isPalindrome(str))
cout << "Palindrome";
else
cout << "Not a Palindrome";
return 0;
}
🔹 Output
Palindrome
🔟 Remove Duplicate String in C++
📌 Removing duplicate characters.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
string removeDuplicates(string str) {
unordered_set<char> seen;
string result;
for (char ch : str) {
if (seen.find(ch) == seen.end()) {
seen.insert(ch);
result += ch;
}
}
return result;
}
int main() {
string str = "programming";
cout << "After removing duplicates: " << removeDuplicates(str);
return 0;
}
🔹 Output
After removing duplicates: progamin
1️⃣1️⃣ String Resize in C++
📌 Resizing a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
str.resize(5);
cout << str;
return 0;
}
🔹 Output
Hello
1️⃣2️⃣ String Reverse without using Function
📌 Manually reversing a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello";
int n = str.length();
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
cout << "Reversed String: " << str;
return 0;
}
🔹 Output
Reversed String: olleh
1️⃣3️⃣ Sentence Case Example using String
📌 Capitalizing the first letter.
#include <iostream>
#include <string>
using namespace std;
string sentenceCase(string str) {
if (!str.empty())
str[0] = toupper(str[0]);
return str;
}
int main() {
string str = "hello world!";
cout << sentenceCase(str);
return 0;
}
🔹 Output
Hello world!
1️⃣4️⃣ Swapping String in C++
📌 Swapping two strings.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Apple", str2 = "Banana";
cout << "Before Swap: " << str1 << " & " << str2 << endl;
str1.swap(str2);
cout << "After Swap: " << str1 << " & " << str2 << endl;
return 0;
}
🔹 Output
Before Swap: Apple & Banana
After Swap: Banana & Apple
📌 Summary
✔ Strings in C++ are mutable and support various functions.
✔ Use .append()
, +
, or +=
for concatenation.
✔ Use .length()
or .size()
to find the length.
✔ Use .resize()
to adjust the string size.
✔ Use getline(cin, str)
for full-line input.
✔ swap()
, reverse()
, and removeDuplicates()
are useful manipulations.
🚀 Let me know if you need more examples! 🔥
0 Comments