Question

Write a C++ program that reads in input until the EOF. The program should output the...

Write a C++ program that reads in input until the EOF.

The program should output the input exactly...with the following changes:

Anytime the substring "New York University", "American", or "Lady gaga" appears in the text, it should be replaced with 5 asterisks "*****".

Example:

Input:

New York University safmsa qwqoca  

safffja qwrq ssss 23333

af21jspof American shfafh121 Lady gaga spdj1-

(Note that this is a paragraph with ONE line in the middle)

Output:

***** safmsa qwqoca

safffja qwrq ssss 23333

af21jspof ***** shfafh121 ***** spdj1-

0 0
Add a comment Improve this question Transcribed image text
Answer #1

below is the detailed explanation I hope you will understand it properly

I am reading data from the data file

#include<bits/stdc++.h>
using namespace std;
int main(){
ifstream file; //input stream
file.open("data");//opening a file
string s;
string str = "";
while(file>>s){ //reading data and writing it to s
str+=s+" "; //storing s to str
}
//it's our actual string
cout<<"print the actual string\n";
cout<<str<<"\n\n";
  
//Lets first check for New York University
//and its count is 19
string str1 = "New York University";// first substring
size_t index = str.find(str1); //finding the first index of str1(New York University)
while(index != string::npos){ // if index is not equal to the end of the str then we have found the substring
string temp = str.substr(0,index); //storing values from 0 to the first index
string x = "*****"; //adding 5 starts to the x
temp+=x; //adding it to the temp which is a temporary variable
  
int start = index+str1.size(); // now we need to skip str1 or(New York University) from our actual string becuase we have replaced it with 5 starts


// New York University starting index


int end = str.size()-start; //New York University ending index so we skiped this string from start to end
  
temp+=str.substr(start,end); //now adding rest of the string to temp which we need
str = "";
str = temp; // now assigning temp to str
index = str.find(str1); // finding the index again if any exist

  
}
// printing our string without str1
cout<<"after removing New York University \n";
cout<<str<<"\n\n";
  
//same is the case for ther substring so i am not writing comments for them i hope you understands
// .......................////////////////////////////////////////////////..................
//now check for "American"
str1 = "American";
index = str.find(str1);
while(index != string::npos){
//string ::npos it return the maximum possible value that can be stored in size_t if the substring is not find it is beeter way to check
string temp = str.substr(0,index);
string x = "*****";
temp+=x;
  
int start = index+str1.size();
  
int end = str.size()-start;
  
temp+=str.substr(start,end);
str = "";
str = temp;
index = str.find(str1);
}
cout<<"after removing American"<<endl;
cout<<str<<"\n\n";
  
// .....................................///////////////////////////////////////////////.....................
//now check for Lady gaga
str1 = "Lady gaga";
index = str.find(str1);
while(index != string::npos){
string temp = str.substr(0,index);
string x = "*****";
temp+=x;
  
int start = index+str1.size();
  
int end = str.size()-start;
  
temp+=str.substr(start,end);
str = "";
str = temp;
index = str.find(str1);
}
cout<<"after removing Lady gaga"<<endl;
cout<<str<<endl;
}

output file

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads in input until the EOF. The program should output the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT