Answer to Question #54041 in C++ for B.Priyanga

Question #54041
Problem : Dictionary

A big data scientist is developing a certain algorithm to make searching easier. To develop this algorithm, the scientist needs a list of all words whose first three characters match in the given input text. You are chosen to assist the scientist. Write a program that processes text and produces matching words. The output criteria and examples will help you in understand what is expected out of your program.
1
Expert's answer
2015-08-11T03:21:54-0400
Answer

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

vector<string> find_words(vector<string> vs,string search)
{
vector<string>res;
for (int k=0;k<vs.size();k++)
{
bool yes=1;
for (int i=0;i<search.length();i++)
if (search[i]!=vs[k][i])
yes=0;
if (yes)
res.push_back(vs[k]);
}
return res;
}

int main()
{
string dic_name("dictionary.txt");
string search;
cout<<"Enter the first letters: ";
cin>>search;
ifstream dict;
dict.open(dic_name);
vector<string>vs;
for (string s;dict>>s;)
vs.push_back(s);
dict.close();
cout<<"Words:\n\n";
for (auto s:find_words(vs, search))
cout<<s<<endl;
}
dictionary.txt
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaah
aaaaaaauugh
aaaaaagh
aaaaaahhhhh
aaaaaaugh
aaaaagh
aaaaah
aaaarthur
aaaaw
aaagghhhh
aaah
aaaugh
aaccf
aachen
aacr
aadland
aaem
aagate
aage
aagh
aahed
aahing
aahs
aahz
aaimasa
aaiun
aaker
aakhwe
aalders
aaleira
aalen
aali
aalii
aaltje
aalto
aamers
aames
aamrl
aana
aand
aani
aappmedic
aara
aarai
aaran
aardvark
aardvarks
aardwolf
aaren
aargau
aargh
aarhus
aari
aarika
aariya
aarnet
aaron
aaronic
aaronical
aaronite
aaronites
aaronitic
aarons
aaronsburg
aaronson
aarthur
aartjan
aaru
aaryn
aase
aasen
aastveit
aasvogel
aaugh
aavasaksa
aaye
aazq
ababdeh
ababua
abac
abaca
abacab
abacama
abacate
abacay
abacha
abachidze
abaci
abacinate
abacination
abaciscus
abacist
aback
abaco
abactinal
abactinally
abaction
abactor
abaculus
abacus
abacuses
aba

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS