Program: String Manipulation using Functions After append: HelloWorld
#include <iostream> Length of result: 10
#include <string> After insert: Hello BeautifulWorld
using namespace std; After replace: Hello WonderfulWorld
int main() { After erase: HelloWorld
string str1 = "Hello"; 'World' found at position: 5
string str2 = "World"; Substring (0–5): Hello
// 1. append() - add str2 to str1 🔧 Functions Used:
string result = str1.append(str2); Function Description
cout << "After append: " << result << endl; append() Joins another string
length() Returns number of characters
// 2. length() - get length of the string insert() Adds characters at specified position
cout << "Length of result: " << result.length() << endl;
replace() Replaces part of the string
// 3. insert() - insert text into string
result.insert(5, " Beautiful"); erase() Deletes characters from string
cout << "After insert: " << result << endl; find() Finds the index of a word
// 4. replace() - replace part of string substr() Extracts a portion of the string
result.replace(6, 9, "Wonderful"); Great! Let's explore the additional string programs one by one:
cout << "After replace: " << result << endl;
// 5. erase() - erase characters from string ✅ 1. String Comparison in C++
result.erase(5, 9); Compare two strings using == or .compare().
cout << "After erase: " << result << endl; #include <iostream>
// 6. find() - find a word #include <string>
size_t pos = result.find("World"); using namespace std;
if (pos != string::npos) int main() {
cout << "'World' found at position: " << pos << endl; string str1 = "apple";
else string str2 = "orange";
cout << "'World' not found" << endl; // Using == operator
// 7. substr() - get part of the string if (str1 == str2)
string sub = result.substr(0, 5); cout << "Strings are equal" << endl;
cout << "Substring (0–5): " << sub << endl; else
return 0; cout << "Strings are not equal" << endl;
} // Using compare() function
🔍 Output Explanation: if (str1.compare(str2) == 0)
cout << "Strings are equal" << endl;
else return 0;
cout << "Strings are not equal" << endl; }
return 0;
} ✅ 4. Count Vowels and Consonants
✅ 2. Palindrome Checker using String #include <iostream>
Check if the string reads the same backward. #include <string>
#include <iostream> using namespace std;
#include <string> int main() {
using namespace std; string str;
int main() { int vowels = 0, consonants = 0;
string str, rev; cout << "Enter a string: ";
cout << "Enter a string: "; getline(cin, str);
cin >> str; for (char ch : str) {
// Reverse the string ch = tolower(ch);
rev = string(str.rbegin(), str.rend()); if (isalpha(ch)) {
if (str == rev) if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
cout << str << " is a palindrome." << endl; vowels++;
else else
cout << str << " is not a palindrome." << endl; consonants++;
return 0;} }
✅ 3. Reverse a String }
Simply print the string in reverse. cout << "Vowels: " << vowels << "\nConsonants: " << consonants
#include <iostream> << endl;
#include <string> return 0;
using namespace std; }
int main() { ✅ 5. Convert to Uppercase and Lowercase
string str; #include <iostream>
cout << "Enter a string: "; #include <string>
cin >> str; using namespace std;
// Using loop to reverse int main() {
cout << "Reversed string: "; string str;
for (int i = str.length() - 1; i >= 0; i--) { cout << "Enter a string: ";
cout << str[i]; getline(cin, str);
} // Convert to uppercase
cout << endl; for (char &c : str) {
c = toupper(c); if (!ispunct(c))
} result += c; }
cout << "Uppercase: " << str << endl; cout << "Without punctuation: " << result << endl;
// Convert to lowercase return 0;
for (char &c : str) { }
c = tolower(c);
} ✅ 8. Check if Two Strings are Anagrams
cout << "Lowercase: " << str << endl; #include <iostream>
return 0; #include <string>
} #include <algorithm>
✅ 6. Remove Spaces from a String using namespace std;
#include <iostream> int main() {
#include <string> string str1, str2;
using namespace std; cout << "Enter first string: ";
int main() { cin >> str1;
string str, noSpace = ""; cout << "Enter second string: ";
cout << "Enter a string: "; cin >> str2;
getline(cin, str);
for (char c : str) { sort(str1.begin(), str1.end());
if (c != ' ') sort(str2.begin(), str2.end());
noSpace += c; if (str1 == str2)
} cout << "The strings are anagrams." << endl;
cout << "String without spaces: " << noSpace << endl; else
return 0; cout << "The strings are not anagrams." << endl;
}
✅ 7. Remove Punctuation from a String return 0;
#include <iostream> }
#include <string> ✅ 9. Count Words in a String
using namespace std; #include <iostream>
int main() { #include <string>
string str, result = ""; #include <sstream>
cout << "Enter a string with punctuation: "; using namespace std;
getline(cin, str);
int main() {
for (char c : str) { string sentence;
cout << "Enter a sentence: ";
getline(cin, sentence); cout << "Concatenated: " << result << endl;
stringstream ss(sentence); return 0;
string word; }
int count = 0;
while (ss >> word) { ✅ 12. Toggle Case of a String
count++; #include <iostream>
} #include <string>
cout << "Number of words: " << count << endl; using namespace std;
return 0; int main() {
} string str;
cout << "Enter a string: ";
✅ 10. Check Substring Exists getline(cin, str);
#include <iostream> for (char &c : str) {
#include <string> if (islower(c))
using namespace std; c = toupper(c);
int main() { else if (isupper(c))
string text = "HelloWorld"; c = tolower(c);
string sub; }
cout << "Enter substring to search: "; cout << "Toggled string: " << str << endl;
cin >> sub; return 0;
}
if (text.find(sub) != string::npos)
cout << "Substring found!" << endl; ✅ 13. Remove Duplicate Characters
else #include <iostream>
cout << "Substring not found." << endl; #include <string>
return 0; #include <unordered_set>
} using namespace std;
✅ 11. Concatenate Two Strings Without append() int main() {
#include <iostream> string str, result = "";
#include <string> unordered_set<char> seen;
using namespace std; cout << "Enter a string: ";
int main() { getline(cin, str);
string str1 = "Good", str2 = "Morning";
string result = str1 + " " + str2; for (char c : str) {
if (seen.find(c) == seen.end()) { return false;
seen.insert(c); }
result += c; return true;
} }
} int main() {
cout << "String without duplicates: " << result << endl; string input;
return 0; cout << "Enter a string: ";
} cin >> input;
✅ 14. Find Frequency of Each Character
#include <iostream> if (isNumeric(input))
#include <string> cout << "It's numeric." << endl;
#include <map> else
using namespace std; cout << "It's not numeric." << endl;
int main() {
string str; return 0;
map<char, int> freq; }
cout << "Enter a string: ";
getline(cin, str); ✅ 1. Login System (Username & Password Validation)
for (char c : str) { #include <iostream>
if (c != ' ') #include <string>
freq[c]++; using namespace std;
} int main() {
for (auto pair : freq) { string username = "admin";
cout << pair.first << " = " << pair.second << endl; string password = "1234";
} string inputUser, inputPass;
return 0; cout << "Enter username: ";
} cin >> inputUser;
✅ 15. Check if String is Numeric cout << "Enter password: ";
cin >> inputPass;
#include <iostream> if (inputUser == username && inputPass == password)
#include <string> cout << "Login successful!" << endl;
using namespace std; else
bool isNumeric(string str) { cout << "Invalid credentials!" << endl;
for (char c : str) {
if (!isdigit(c)) return 0;
} stringstream ss(text);
✅ 2. Sort Names Alphabetically string word;
#include <iostream> while (ss >> word) {
#include <string> freq[word]++;
#include <vector> }
#include <algorithm> cout << "Word Frequencies:\n";
using namespace std; for (auto pair : freq) {
int main() { cout << pair.first << " = " << pair.second << endl;
int n; }
cout << "Enter number of names: ";
cin >> n; return 0;
vector<string> names(n); }
cout << "Enter the names:" << endl;
for (int i = 0; i < n; i++) { ✅ 4. Email Validation (Basic Check)
cin >> names[i]; #include <iostream>
} #include <string>
sort(names.begin(), names.end()); using namespace std;
cout << "Sorted names:" << endl;
for (string name : names) { bool isValidEmail(string email) {
cout << name << endl; size_t at = email.find('@');
} size_t dot = email.find('.', at);
return 0; return (at != string::npos && dot != string::npos && dot > at);
} }
✅ 3. Count Frequency of Each Word int main() {
#include <iostream> string email;
#include <string> cout << "Enter an email address: ";
#include <sstream> cin >> email;
#include <map> if (isValidEmail(email))
using namespace std; cout << "Valid email!" << endl;
int main() { else
string text; cout << "Invalid email format." << endl;
cout << "Enter a sentence: "; return 0;
getline(cin, text); }
map<string, int> freq; ✅ 5. Student Record System (Name + Marks + Grade)
s.display();
return 0;
#include <iostream> }
#include <string> ✅ 1. Password Masking in C++ (Console-based)
using namespace std; 📌 This hides the password as you type (shows * instead of real
characters). Works in Windows (with <conio.h>).
class Student { #include <iostream>
public: #include <conio.h> // For _getch()
string name; #include <string>
int mark; using namespace std;
int main() {
void input() { string username = "admin";
cout << "Enter name: "; string password = "1234";
cin >> name; string inputUser, inputPass;
cout << "Enter marks: "; cout << "Enter username: ";
cin >> mark; cin >> inputUser;
}
cout << "Enter password: ";
void display() { char ch;
cout << "Name: " << name << "\nMarks: " << mark; while ((ch = _getch()) != '\r') { // '\r' is Enter key
if (mark >= 90) if (ch == '\b') { // backspace
cout << "\nGrade: A"; if (!inputPass.empty()) {
else if (mark >= 75) inputPass.pop_back();
cout << "\nGrade: B"; cout << "\b \b";
else if (mark >= 50) }
cout << "\nGrade: C"; } else {
else inputPass += ch;
cout << "\nGrade: F"; cout << "*";
cout << endl; }
} }
}; cout << endl;
int main() { if (inputUser == username && inputPass == password)
Student s; cout << "Login successful!" << endl;
s.input(); else
cout << "Invalid username or password!" << endl; cout << "Reversed: " << str << endl;
return 0; break;
} case 4:
⚠️Note: This works in Windows only due to conio.h and _getch(). cout << "Length: " << str.length() << endl;
✅ 2. Menu-Driven String ApplicationA simple menu to perform break;
multiple string operations using a loop. case 5:
#include <iostream> cout << "Exiting program.\n";
#include <string> break;
#include <algorithm> default:
using namespace std; cout << "Invalid choice. Try again.\n";
int main() { }
string str; } while (choice != 5);
int choice;
cout << "Enter a string: "; return 0;
getline(cin, str); }
do { ✅ 1. Replace a Word in a String
cout << "\n--- String Menu ---\n"; Replaces a specific word with another using find() and replace().
cout << "1. Convert to UPPERCASE\n";
cout << "2. Convert to lowercase\n";
cout << "3. Reverse the string\n"; #include <iostream>
cout << "4. Length of string\n"; #include <string>
cout << "5. Exit\n"; using namespace std;
cout << "Enter your choice: ";
cin >> choice; int main() {
switch (choice) { string str, word, replacement;
case 1:
for (char &c : str) c = toupper(c); cout << "Enter a sentence: ";
cout << "Uppercase: " << str << endl; getline(cin, str);
break;
case 2: cout << "Enter the word to replace: ";
for (char &c : str) c = tolower(c); cin >> word;
cout << "Lowercase: " << str << endl; cout << "Enter the replacement word: ";
break; cin >> replacement;
case 3: size_t pos = str.find(word);
reverse(str.begin(), str.end());
if (pos != string::npos) { return 0;
str.replace(pos, word.length(), replacement); }
cout << "Updated sentence: " << str << endl; 🔍 Example:
} else { Input: This is a test
cout << "Word not found in the string." << endl; Output: Thsstst
}
return 0;} ✅ Full Menu-Driven String Program
🔍 Example:
Input: #include <iostream>
Sentence: I love cats #include <string>
Word to replace: cats #include <algorithm>
Replacement: dogs using namespace std;
Output: I love dogs bool isVowel(char c) {
c = tolower(c);
✅ 2. Remove Vowels and Spaces return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
Deletes all vowels (a, e, i, o, u) and spaces from the input. }
#include <iostream> int main() {
#include <string> string str;
using namespace std; int choice;
bool isVowel(char c) { cout << "Enter a string: ";
c = tolower(c); getline(cin, str);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; do {
} cout << "\n---- String Operations Menu ----\n";
int main() { cout << "1. Convert to UPPERCASE\n";
string str, result = ""; cout << "2. Convert to lowercase\n";
cout << "Enter a string: "; cout << "3. Reverse the string\n";
getline(cin, str); cout << "4. Find length of string\n";
cout << "5. Replace a word\n";
for (char c : str) { cout << "6. Remove vowels\n";
if (!isVowel(c) && c != ' ') { cout << "7. Remove spaces\n";
result += c; cout << "8. Exit\n";
} cout << "Enter your choice (1–8): ";
} cin >> choice;
cin.ignore(); // to clear the input buffer
cout << "After removing vowels and spaces: " << result << endl;
switch (choice) { if (!isVowel(c)) result += c;
case 1: }
for (char &c : str) c = toupper(c); str = result;
cout << "Uppercase: " << str << endl; cout << "String after removing vowels: " << str << endl;
break; break;
case 2: }
for (char &c : str) c = tolower(c); case 7: {
cout << "Lowercase: " << str << endl; string result = "";
break; for (char c : str) {
case 3: if (c != ' ') result += c;
reverse(str.begin(), str.end()); }
cout << "Reversed string: " << str << endl; str = result;
break; cout << "String after removing spaces: " << str << endl;
case 4: break;
cout << "Length of the string: " << str.length() << endl; }
break; case 8:
case 5: { cout << "Exiting program. Goodbye!\n";
string word, replacement; break;
cout << "Enter word to replace: "; default:
getline(cin, word); cout << "Invalid choice. Try again.\n";
cout << "Enter replacement word: "; }
getline(cin, replacement); } while (choice != 8);
return 0;
size_t pos = str.find(word); }
if (pos != string::npos) {
str.replace(pos, word.length(), replacement);
cout << "Updated string: " << str << endl;
} else {
cout << "Word not found!" << endl;
}
break;
}
case 6: {
string result = "";
for (char c : str) {