C++ CODING - Micosoft Visual Studio please Write a word search program that sear
ID: 3904073 • Letter: C
Question
C++ CODING - Micosoft Visual Studio please
Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of characters in the input data file that are not vowels. Your program must do this by providing the following functions : void processFile(ifstream inFile, string &word, int &wordCount, int &nonVowelCount) ; void displayResult(string word, int wordCount, int nonVowelCount) ; Both functions should be called from main(). No non-constant global variables should be used. Test your program using the file provided “paragraph.dat”.
Explanation / Answer
PROGRAM
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
char ch; // declare non constant global character variable
// implement processFile() function
void processFile(ifstream &inFile,string &word,int &wordCount,int &nonVowelCount)
{
if(inFile.bad()) // check condition if file is not avaiable
{
cout<<"Error: Open File... "; // display error message
exit(0); // then terminate from entire program
}
char str[10]; // declare character array str
int count=0; // declare integer count=0 variable
while(inFile.good()) // create while loop until end of file
{
inFile>>ch; // read each characters
cout<<ch; // display entire file content
if(ch!='a'&&ch!='i'&&ch!='e'&&ch!='o'&&ch!='u') // check condition weather the file contains not vowels
nonVowelCount++; // then counting characters
count++; // this varaible increment number of characters in the file
}
inFile.close(); // close this file
cout<<endl;
inFile.open("f:\paragraph.dat"); // again open in read mode of this file
while(inFile>>str) // create while loop and read each word in the file
{
if(str==word) // check condition the word is available or not
wordCount++; // then count number of words
}
cout<<" Total Characters: "<<count<<endl; // display total characters in the file
inFile.close(); // finally close this file
}
// implement displayResult() function
void displayResult(string word,int wordCount,int nonVowelCount)
{
// display wordCount and nonVowelCount
cout<<"Total "<<"'"<<word<<"'"<<" is: "<<wordCount<<endl;
cout<<"Non Vowel Count: "<<nonVowelCount<<endl;
}
int main()
{
ifstream inFile; // Create ifstream Object inFile
inFile.open("f:\paragraph.dat"); // open file in read mode
string str; // declare string variable
cout<<" Enter Searching Word: ";
cin>>str; // read word
int x=0,y=0; // declare two integer variable x and y
processFile(inFile,str,x,y); // calling processFile() function
displayResult(str,x,y); // calling displayResult() function
inFile.close(); // close this file
return 0;
}
OUTPUT-1
Enter Searching Word: word
Writeawordsearchprogramthatsearchesaninputdatafileforawordspecifiedbytheuser.Theprogramshoulddisplaythenumberoftimesthewordappearsintheinputdatafile.Inaddition,theprogramshouldcountanddisplaythenumberofcharactersintheinputdatafilethatarenotvowels.Yourprogrammustdothisbyprovidingthefollowingfunctionss
Total Characters: 301
Total 'word' is: 3
Non Vowel Count: 194
OUTPUT-2
Enter Searching Word: program
Writeawordsearchprogramthatsearchesaninputdatafileforawordspecifiedbytheuser.Theprogramshoulddisplaythenumberoftimesthewordappearsintheinputdatafile.Inaddition,theprogramshouldcountanddisplaythenumberofcharactersintheinputdatafilethatarenotvowels.Yourprogrammustdothisbyprovidingthefollowingfunctionss
Total Characters: 301
Total 'program' is: 4
Non Vowel Count: 194
UPDATED PROGRAM
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
char ch; // declare non constant global character variable
// implement processFile() function
void processFile(ifstream &inFile,string &word,int &wordCount,int &nonVowelCount)
{
if(inFile.bad()) // check condition if file is not avaiable
{
cout<<"Error: Open File... "; // display error message
exit(0); // then terminate from entire program
}
char str[10]; // declare character array str
int count=0; // declare integer count=0 variable
int wordCount1=0; // declare integer wordCount1=0 variable
string word1; // declare string word1
while(inFile>>word1) // read words from the file
{
wordCount1++; // increment counter
}
inFile.close(); // close file
inFile.open("f:\paragraph.dat"); // again open in read mode of this file
while(inFile.good()) // create while loop until end of file
{
inFile.get(ch); // read each characters
cout<<ch; // display entire file content
if(ch!='a'&&ch!='i'&&ch!='e'&&ch!='o'&&ch!='u') // check condition weather the file contains not vowels
nonVowelCount++; // then counting characters
count++; // this varaible increment number of characters in the file
}
inFile.close(); // close this file
cout<<endl;
inFile.open("f:\paragraph.dat"); // again open in read mode of this file
while(inFile>>str) // create while loop and read each word in the file
{
if(str==word) // check condition the word is available or not
wordCount++; // then count number of words
}
cout<<" Total Characters: "<<count<<endl; // display total characters in the file
cout<<" Word Count: "<<wordCount1<<endl;
inFile.close(); // finally close this file
}
// implement displayResult() function
void displayResult(string word,int wordCount,int nonVowelCount)
{
// display wordCount and nonVowelCount
cout<<"Total "<<"'"<<word<<"'"<<" is: "<<wordCount<<endl;
cout<<"Non Vowel Count: "<<nonVowelCount<<endl;
}
int main()
{
ifstream inFile; // Create ifstream Object inFile
inFile.open("f:\paragraph.dat"); // open file in read mode
string str; // declare string variable
cout<<" Enter Searching Word: ";
cin>>str; // read word
int x=0,y=0; // declare two integer variable x and y
processFile(inFile,str,x,y); // calling processFile() function
displayResult(str,x,y); // calling displayResult() function
inFile.close(); // close this file
return 0;
}
OUTPUT
Enter Searching Word: program
Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of characters in the input data file that are not vowels. Your program must do this by providing the following functions.
Total Characters: 368
Word Count: 65
Total 'program' is: 4
Non Vowel Count: 261
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.