BS(IT),SEMESTER#01
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
TASKS
Task 1: GREETING CARD COMPANY
Imagine a greeting card company that allows customers to personalize their greeting
cards with custom messages. One of the features provided by the company is the ability
to replace specific words in their messages with other words.
This is particularly useful for creating a consistent theme across multiple cards, such as
changing "Birthday" to "Anniversary" or "Congratulations" to "Well Done". The
company has tasked you with developing a software solution that will allow customers
to easily make these changes to their messages.
CODE:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void replaceWord(string &message, const string &oldWord, const string &newWord) {
size_t position = 0;
while ((position = message.find(oldWord, position)) != string::npos) {
message.replace(position, oldWord.length(), newWord);
, BS(IT),SEMESTER#01
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
position += newWord.length();
}
}
int main() {
string message;
int numReplacements;
cout << "Enter your greeting message: ";
getline(cin, message);
cout << "How many words would you like to replace? ";
cin >> numReplacements;
cin.ignore();
for (int i = 0; i < numReplacements; ++i) {
string oldWord, newWord;
cout << "Enter the word to replace: ";
getline(cin, oldWord);
cout << "Enter the replacement word: ";
getline(cin, newWord);
replaceWord(message, oldWord, newWord);
}
cout << "\nUpdated Greeting Message: " << message << endl;
return 0;
}
Output:
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
TASKS
Task 1: GREETING CARD COMPANY
Imagine a greeting card company that allows customers to personalize their greeting
cards with custom messages. One of the features provided by the company is the ability
to replace specific words in their messages with other words.
This is particularly useful for creating a consistent theme across multiple cards, such as
changing "Birthday" to "Anniversary" or "Congratulations" to "Well Done". The
company has tasked you with developing a software solution that will allow customers
to easily make these changes to their messages.
CODE:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void replaceWord(string &message, const string &oldWord, const string &newWord) {
size_t position = 0;
while ((position = message.find(oldWord, position)) != string::npos) {
message.replace(position, oldWord.length(), newWord);
, BS(IT),SEMESTER#01
UROOJ ASGHAR KHAN
O2-135242-006
02-135242-006
position += newWord.length();
}
}
int main() {
string message;
int numReplacements;
cout << "Enter your greeting message: ";
getline(cin, message);
cout << "How many words would you like to replace? ";
cin >> numReplacements;
cin.ignore();
for (int i = 0; i < numReplacements; ++i) {
string oldWord, newWord;
cout << "Enter the word to replace: ";
getline(cin, oldWord);
cout << "Enter the replacement word: ";
getline(cin, newWord);
replaceWord(message, oldWord, newWord);
}
cout << "\nUpdated Greeting Message: " << message << endl;
return 0;
}
Output: