Question

For C++, how do I read through a file called "dictionary.txt" and see if the word...

For C++, how do I read through a file called "dictionary.txt" and see if the word user-inputted is in the file, but using cstring only and NOT string.

For example,

if the dictionary.txt is like

Another

ostentatious

work

extra

dog

camera

hinder

the output should look like this,

Enter a word to be looked up in dictionary: cats

cats is NOT in the dictionary.

Enter a word to be looked up in dictionary: ostentatious

ostentatious IS in the dictionary.

Homework Answers

Answer #1

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
    ifstream infile;
    char str[20],temp[20];
    int i,opt;
    infile.open("dictionary.txt"); //open the file dictionary.txt
    if(!infile)
    {
        cout<<endl<<"Unable to open the file";
        return 0;
   }
    cout<<endl<<"Enter a word to be looked u in dictionary : ";
    cin>>temp; //read the word to lookup in dictionary
    //loop to read the file till end
    while(!infile.eof())
    {
       infile>>str;//read a word from file
       //compare the word with the word in file
       for(i=0;temp[i]!='\0';i++)
           if(temp[i]!=str[i])
               break;
       if(temp[i]=='\0' && str[i]=='\0')  
       {
           cout<<endl<<temp<<" is in the dictionary.";
           opt=1;
           break;
       }
   }
   if(opt!=1)
   {
       cout<<endl<<temp<<" is not in the dictionary.";
   }
    infile.close();//close the file
}

output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Read the words in from the binary file and figure out how many times each word...
Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user. Use ObjectInputStream to read binary file Use a HashMap with the word as a key (String) and an Integer as the value. For each word, first check to see if it already exists in the Map. If not, add the word as key with a value of 1 for the Integer value. If it...
Problem: Read in a word and display the requested characters from that word in the requested...
Problem: Read in a word and display the requested characters from that word in the requested format. Write a Java program that ● prompts the user for a word and reads it, ● converts all characters of the input word to upper case and displays every character whose index is a multiple of 3 starting from the 1st one, ● next converts all characters of the input word to lower case and displays the word with the characters in reverse...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
How would I make a linked list of a linked list in C? For example I...
How would I make a linked list of a linked list in C? For example I want a linked list that holds all the words inputted from the user. Example: typedef struct node{ char *word; struct node *next; }node; This list will hold all the strings that were inputted from the user. What I want to do is have another linked list that holds all the words that are no vowel matches which means that they would be equal if...
Python Regular Expressions problem: How do you get string data from file of say rows of...
Python Regular Expressions problem: How do you get string data from file of say rows of IP address and city location values obtained from re.finditer patterns to appear in the same dictionary? for example: text file has 100.200.10.255 New York City 10. 16. 25.254 Los Angeles segment of code includes for item in re.finditer(the_two_patterns, text_file, re.MULTILINE): print (item.groupdict()) result is below that I am getting {‘IP’: ‘100.200.10.255’, ‘city’: None} {‘IP’: None, ‘city’: ‘New York City’} {‘IP’: ‘10.16.25.254’, ‘city’: None} {IP’:...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT