C++ programming I need help finding a search word and the longestword in a data
ID: 3610706 • Letter: C
Question
C++ programming I need help finding a search word and the longestword in a data file. Example Program output Enter inputfile name data09 Enter search string ill The reformatted content of data09 is: Jack and Jill went up the hIll 2 fetch a pail ofstill water. Jack fell down and broke his crown
and Jill came tumbling after! willjillbeill? # of times ill found: 6 longest word: willjillbeill? it should not count Ill in hIllbecause the "i" is capitalized data09 is a filestream. *I am not allowed to usearrays I need to add it to my program. This is my program sofar: #include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
ifstream infile; //input filestream variable
string FileName; //variable to hold file name
int count,prevcount=0,max=50;
string word;
bool notfirst=false,firstword=true;
cout<<"Enter input file name"<<endl;
cin>>FileName;
infile.open(FileName.c_str());
infile>>word;
while(infile)
{count=word.length();
if(count+prevcount>max)
{cout<<endl;
prevcount=0;
notfirst=false;
}
else
notfirst=true;
prevcount+=count+1;
if(notfirst&&!firstword)
cout<<" ";
firstword=false;
cout<<word;
infile>>word;
}
cout<<endl;
return 0;
} C++ programming I need help finding a search word and the longestword in a data file. Example Program output Enter inputfile name data09 Enter search string ill The reformatted content of data09 is: Jack and Jill went up the hIll 2 fetch a pail of
still water. Jack fell down and broke his crown
and Jill came tumbling after! willjillbeill? # of times ill found: 6 longest word: willjillbeill? it should not count Ill in hIllbecause the "i" is capitalized data09 is a filestream. *I am not allowed to usearrays I need to add it to my program. This is my program sofar: #include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
ifstream infile; //input filestream variable
string FileName; //variable to hold file name
int count,prevcount=0,max=50;
string word;
bool notfirst=false,firstword=true;
cout<<"Enter input file name"<<endl;
cin>>FileName;
infile.open(FileName.c_str());
infile>>word;
while(infile)
{count=word.length();
if(count+prevcount>max)
{cout<<endl;
prevcount=0;
notfirst=false;
}
else
notfirst=true;
prevcount+=count+1;
if(notfirst&&!firstword)
cout<<" ";
firstword=false;
cout<<word;
infile>>word;
}
cout<<endl;
return 0;
}
Explanation / Answer
#include #include#include#includeusing namespace std;int main(){ifstream infile; //input filestream variablestring FileName; //variable to hold file nameint count,prevcount=0,max=50;string word;string max_word,search_word;;int max_len = 0,count_word=0;bool notfirst=false,firstword=true;coutword;while(infile){if(word==search_word){count_word++;}else{size_t c_pos=0,pos=0;while((c_pos=word.find(search_word,pos))!=-1){pos = c_pos+1;count_word++;}}count=word.length();if(word.length()>max_len){max_len = word.length();max_word = word;}if(count+prevcount>max){coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.