Question: IN C++ language I have the main code frameworked below. I need help fixing one part of the code when using the search special character

IN C++ language

I have the main code frameworked below. I need help fixing one part of the code when using the search special character '?'

How can I get the code to only find words when the special character is used the amount of times used by the user.

For example with the 'sample.txt' when you input a?? it should only say "the word and was found one time"

But how i have it now it finds the words 'and' 'as' & 'a'. But I only need it to find the word and as a?? wants to find the words starting with a and missing two characters

Description For this project you will read words from a given file into your program. Each word consists strictly of the letters A-Z and a-z. Any whitespace, punctuation, digit, etc. will separate words. An example would be this!is A&Word. This text would represent the 4 words this, is, A, and Word. Each word should be converted to lowercase so that only lowercase letters remain. Once you have read all of the words, you should output the total number of words read. Your next task is to determine how many distinct words appear in the file and output that value. Finally, you will prompt the user to enter a word and report how many occurrences of the given word are in the file. You will continue to prompt the user until they enter the word END in uppercase. When the user enters a word to search for, a ? character may be present as some of the letters for the search. This special character represents the ability to match any non-empty character. For example, foo? matches both "foot" and "food". This query should report every word that matches. Keep in mind that the ? character in the source input file is not a wildcard match. In the original source, it is just another non-letter character. Requirements Please carefully read the following requirements: You must supply a makefile that compiles your code to produce the project1 executable. You must use C++ streams for all I/O. You must format your output as shown in the example below. You must do your own work, you must not share code. You must submit your project in a zipfile as specified under submission.

Example Here is an example document sample.txt Cryptography is both the practice and study of the techniques used to communicate and/or store information or data privately and securely, without being intercepted by third parties. This can include processes such as encryption, hashing, and steganography. Until the modern era, cryptography almost exclusively referred to encryption, but now cryptography is a broad field with applications in many critical areas of our lives.

You must be able to run the program as shown below and get the identical output $ make g++ -Wall -std=c++11 main.c -o project1 $ ./project1 sample.txt The number of words found in the file was 64 The number of unique words found in the file was 52 Please enter a word: of The word of appears 2 times in the document Please enter a word: is The word is appears 2 times in the document Please enter a word: or The word or appears 2 times in the document Please enter a word: a?? The word and appears 4 times in the document Please enter a word: i? The word is appears 2 times in the document The word in appears 1 time in the document Please enter a word: END $

// C++ code

#include #include #include #include #include using namespace std;

void search(string arr[10000], int count, string searchstr) { string matched[count]; int countMatch = 0;

for(int x = 0; x < count; x++) { string str = arr[x]; bool checkMatch = false;

if(str.length() <= searchstr.length()) { for(int y = 0; y < (signed) searchstr.length(); y++) {

if(searchstr[y] == str[y]) { checkMatch = true; } else if(searchstr[y] == '?') { checkMatch = true; } else { checkMatch = false; break; } } }

bool hasMatched = false;

for(int pos = 0; pos < countMatch; pos++) { if(str.compare(matched[pos]) == 0) {

hasMatched = true; } }

if(!hasMatched) { if(checkMatch) {

int occurences = 0;

for(int z = 0; z < count; z++) { if(str.compare(arr[z]) == 0) { occurences++; } }

cout << "The str " << str << " appears " << occurences << " times in the document" << endl; }

matched[countMatch++] = str;

} }

return; }

int uniquecntr(string arr[10000], int amt) { string uniquearr[amt]; int cntr = 0; bool checkUnique = true;

for(int x = 0; x < amt; x++) {

checkUnique = true;

for(int y = 0; y < amt; y++) { if(arr[x].compare(uniquearr[y]) == 0) { checkUnique = false; } }

if(checkUnique) { uniquearr[cntr++] = arr[x]; } }

return cntr; }

string toLower(string str) { int x; for(x = 0; x < (signed) str.length(); x++) { str[x] = tolower(str[x]); }

return str; }

int main(int argc, char** argv) { ifstream infile; infile.open(argv[1]); if(infile.fail()) return 0;

string arr[10000]; string str; int count = 0; int uniqueCount = 0;

while(infile >> str) {

bool checkSplit = false;

for(int pos = 0; pos < (signed) str.length(); pos++) { if(isalpha(str[pos])) { } else {

arr[count++] = toLower(str.substr(0, pos));

checkSplit = true;

if(str[pos+1] != '\0') { string split; for(int loc = pos+1; loc < (signed) str.length(); loc++) { split += str[loc]; }

arr[count++] = toLower(split); } } }

if(!checkSplit) { arr[count] = toLower(str); count++; } }

uniqueCount = uniquecntr(arr, count);

cout << "The number of words found in the file was " << count << endl; cout << "The number of unique words found in the file was " << uniqueCount << endl; string searchstr = ""; cout << "Please enter a word: "; cin >> searchstr;

while(searchstr.compare("END") != 0) { search(arr, count, toLower(searchstr));

cout << "Please enter a word: "; cin >> searchstr; }

infile.close();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!