The following is an old word puzzle: \"name a common word, besides tremendous, s
ID: 3674146 • Letter: T
Question
The following is an old word puzzle: "name a common word, besides tremendous, stupendous and horrendous, that ends in dous." If you think about this for a while, it will probably come to you. however, we can also solve this puzzle by reading a text file of english words and outputting the word if it contains "dous" at the end. The text file "words.txt" contains a large number of english words, including the word that completes the puzzle. This file is available online with the source code for the book. Write a program that reads each word from the text file and outputs only those containing "dous" at the end to solve the puzzle.
The program is attached below, but I cant compile it. Help
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
bool stringSearch (char[], char[]);
const int size=20;
int main()
{
char words.txt[16];
ifstream in_word;
char word[size];
char searchword[] = "dous";
cout << "Enter file name " <<endl;
cin >> words.txt;
in_word.open (words.txt, ios::in);
cout << "Text File Name: " << words.txt <<endl;
cout << "Dictionary Words Containing the Word " << searchword << endl;
cout << " " << endl <<endl;;
while (!in_word.eof())
{
in_word.getline (word, size);
if (stringSearch(word, searchword))
cout << setw(15) << right << word << endl;
}
return 0;
}
bool stringSearch (char chString[], char searchString[])
{
int index = 0;
int count = 0;
int i = 0;
int len=strlen(searchString);
while (*chString! = '')
{
if (*chString==*searchString){
for (int k=0; k<len; k++){
if (*chString++==*searchString++)
count++;
}
}
else
{
chString++;
cout = 0;
}
if (count == len)
break;
}
if (count == len) return true;
else return false;
}
Explanation / Answer
Hi, I have corrected your code.
Please Test it.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
using namespace std;
bool stringSearch (char[], char[]);
const int size=20;
int main()
{
char fileName[16];
ifstream in_word;
char word[size];
char searchword[] = "dous";
cout << "Enter file name " <<endl;
cin >> fileName;
in_word.open (fileName, ios::in);
cout << "Text File Name: " << fileName <<endl;
cout << "Dictionary Words Containing the Word " << searchword << endl;
cout << " " << endl <<endl;;
while (!in_word.eof())
{
in_word.getline (word, size);
if (stringSearch(word, searchword))
cout << setw(15) << right << word << endl;
}
return 0;
}
bool stringSearch (char chString[], char searchString[])
{
int index = 0;
int count = 0;
int i = 0;
int len=strlen(searchString);
while (*chString != '')
{
if (*chString==*searchString){
for (int k=0; k<len; k++){
if (*chString++==*searchString++)
count++;
}
}
else
{
chString++;
count = 0;
}
if (count == len)
break;
}
if (count == len) return true;
else return false;
}
/*
Output:
Enter file name
words.txt
Text File Name: words.txt
Dictionary Words Containing the Word dous
tremendous
stupendous
horrendous
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.