In C++ Please!!!!!
Example main:
#include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int main() { string results[MAXRESULTS]; string dict[MAXDICTWORDS]; ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("words.txt"); if (!dictfile) { cout << "File not found!" << endl; return (1); } nwords = createDict(dictfile, dict); cout << "Please enter a string for an anagram: "; cin >> word; int numMatches = characterMixer(word, dict, nwords, results); if (!numMatches) cout << "No matches found" << endl; else viewAnswers(results, numMatches); return 0; }
Example word.txt (small version):
10th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th a AAA AAAS Aarhus Aaron AAU ABA Ababa aback abacus abalone abandon abase abash abate abater abbas abbe abbey abbot Abbott abbreviate abc abdicate abdomen abdominal abduct Abe abed Abel Abelian Abelson Aberdeen Abernathy aberrant aberrate abet abetted abetting abeyance abeyant abhorred abhorrent abide Abidjan Abigail abject ablate ablaze able ablution Abner abnormal Abo aboard abode abolish abolition abominable abominate aboriginal aborigine aborning abort abound about above aboveboard aboveground abovementioned abrade Abraham Abram Abramson abrasion abrasive abreact abreast abridge abridgment abroad abrogate abrupt abscess abscissa abscissae absence absent absentee absenteeism absentia absentminded absinthe absolute absolution absolve absorb absorbent absorption absorptive abstain abstention abstinent abstract abstracter abstractor abstruse absurd abuilding abundant abusable abuse abusive abut abutted abutting abysmal abyss Abyssinia AC academe academia academic academician
Working code implemented in C++ and appropriate comments provided for better understanding:
Source code for anagrams.cpp:
#include <iostream>
#include <fstream>
#include <istream>
#include <cstring>
using namespace std;
const int MAXRESULTS = 20; // Max matches that can be
found
const int MAXDICTWORDS = 30000; // Max words that can be read
in
int loadWords(istream & dictfile, string dict[]);
bool findWord(string word,
const string dict[], int top, int bot);
void swapme(char & a, char & b);
void permute(string a, int front, int end,
const string dict[], int dict_total, int & matched, string
results[]);
void Loop(string & a, int i, int front, int end,
const string dict[], int dict_total, int & matched, string
results[]);
int recBlends(string word,
const string dict[], int size, string results[]);
void showResults(const string results[], int size);
bool checkWord(int i, int max, string b, string results[]);
//Adds all the words from dictfile into an array of strings
called dict
int loadWords(istream & dictfile, string dict[]) {
string data;
if (dictfile.eof()) { // end of dictfile
return 0;
}
dictfile >> data;
// uses recursion to loop to the end of dictfile (dict starts with
Z)
int x = loadWords(dictfile, dict);
if (x < MAXDICTWORDS) {
dict[x] = data;
x++;
return x;
} else
return MAXDICTWORDS; //reached max capacity of array
}
//Checks if a word is in the array dict[]
bool findWord(string word,
const string dict[], int top, int bot) {
if (top > bot) {
return false; // string not found
} else {
int mid = (top + bot) / 2;
string check = dict[mid];
check[0] = tolower(check[0]); // makes all words lowecase
if (word.compare(check) == 0) {
return true;
} else if (word.compare(check) > 0) {
return findWord(word, dict, top, mid - 1);
} else if (word.compare(check) < 0) {
return findWord(word, dict, mid + 1, bot);
}
}
return true;
}
//Swaps 2 characters in a word
void swapme(char & a, char & b) {
char temp = a;
a = b;
b = temp;
}
// Finds all the permutations of a word
void permute(string a, int front, int end,
const string dict[], int dict_total, int & matched, string
results[]) {
if (front == end) // found a possible permutation
{
if (findWord(a, dict, 0, dict_total)) // check if word exists
{
if (!(checkWord(0, matched, a, results))) // check if word is
already in results
{
results[matched] = a;
matched++;
}
}
} else {
Loop(a, front, front, end, dict, dict_total, matched,
results);
}
}
void Loop(string & a, int i, int front, int end,
const string dict[], int dict_total, int & matched, string
results[]) {
if (i > end)
return;
// for all remaining letters (i) this loop continues and permutes
all possible anagrams
swapme(a[front], a[i]);
permute(a, front + 1, end, dict, dict_total, matched,
results);
// returns back the switch so that the next loop can swap 2
different characters
swapme(a[front], a[i]);
//replaces the for loop by incrementing i in every call until it
reaches "max" or end
Loop(a, (i + 1), front, end, dict, dict_total, matched,
results);
}
// main function used to find the number of anagrams formed from
a given string
// puts the anagrams in an array called results
int recBlends(string word,
const string dict[], int size, string results[]) {
int back = word.size() - 1;
int matched = 0;
permute(word, 0, back, dict, size, matched, results);
return matched;
}
// Prints all the strings in results
void showResults(const string results[], int size) {
if (size - 1 < 0)
return;
cout << "Matching word " << results[size - 1] <<
endl;
showResults(results, size - 1);
}
// checks if a word exists in results already (avoids doubling ex:
kool gives look twice
bool checkWord(int i, int max, string b, string results[]) {
if (i == max)
return false;
if (b.compare(results[i]) == 0)
return true;
checkWord(i + 1, max, b, results);
return false;
}
int main() {
string results[MAXRESULTS];
string dict[MAXDICTWORDS];
ifstream dictfile; // file containing the list of words
int nwords; // number of words read from dictionary
string word;
dictfile.open("words.txt");
if (!dictfile) {
cout << "File not found!" << endl;
return (1);
}
nwords = loadWords(dictfile, dict);
cout << "Please enter a string for an anagram: ";
cin >> word;
int numMatches = recBlends(word, dict, nwords, results);
if (!numMatches)
cout << "No matches found" << endl;
else
showResults(results, numMatches);
/*
string results3[5];
string exampleDict[] = { "moe", "kool", "dee"};
int numResults = recBlends("kloo", exampleDict, 3, results3);
cout << numResults << endl;
assert(numResults == 1 && results3[0] == "kool");
cout << "woot" << endl;
return 0;
*/
return 0;
}
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.
In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std;...
Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...
Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
Thank you!
/*Lab 8 : Practicing functions and arrays
Purpose:
*/
#include<iostream>
#include<fstream>
using namespace std;
int read_function(int array[], int, int);
int main()
{
int array[300], numba;
read_function(array[300]);
cout << "Enter a whole number between 2-20: " <<
endl;
cin >> numba;
read_function(numba);
return 0;
}
int read_funtion (int arr[300], int num, int Values)
{
int sum=0;
ifstream array_file;
array_file.open("Lab8.dat");
for(int i=0; i < 300; i++)
{
array_file >> arr[i];
cout << arr[i];
sum += i;
}
cout << sum;...
How would answer question #4 of this ?
#1 and 2
#include<iostream>
#include <fstream>
using namespace std;
void read(int arr[])
{
string line;
ifstream myfile("input.txt");
int i = 0;
if (myfile.is_open())
{
while (getline(myfile, line))
{
arr[i];
i++;
}
myfile.close();
}
}
void output(int arr[])
{
ofstream myfile("output.txt");
if (myfile.is_open())
{
int i = 0;...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...
I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]); if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...