Question

Create a program that stores words in an STL vector and then searches the vector for...

Create a program that stores words in an STL vector and then searches the vector for words.

Requirements

You will keep track of strings as C++ string objects.

In main, declare a vector variable to contain your strings.

Create a loop that gets one word at a time from the user until they enter "." as the only thing on the input line. For this assignment, it is adequate to limit strings to one word at a time. Put the word into the vector.

Once the user is done entering words, set up a separate loop to search for words in the vector. Similar to the previous loop, get the searchable words one at a time until they enter ".". When you find the word, use printf() (or cout) to display "Success!". If you don't find the word, use printf() (or cout) to display "Not there!".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

program logic:

  • create a vector<string> named words.
  • prompt user for a string
  • loop until user enters ".":
    • 1.) add the string to words
    • 2.) prompt user for a string
  • now, we have to prompt user for search strings.
  • prompt user for a string
  • loop until user enters "."
    • 1.) check if the string exists in words using std::find() function. If yes, print "success". otherwise, print "Not there"
    • 2.) prompt the user for a string

program:

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

int main(){
   vector<string> words;
   cout<<"Enter the words(enter . to stop): \n";
   string w;
   cin>>w;
   while(w!="."){
       words.push_back(w);
       cin>>w;
   }
   cout<<"Enter search words(enter . to stop): \n";
   string target;
   cin>>target;
   while(target!="."){
       if (find(words.begin(), words.end(),target)!=words.end())
           cout<<"Success\n";
       else
           cout<<"Not there\n";
       cin>>target;
   }
}

Sample inputs and outputs:
Enter the words (enter . to stop): hello sworld a Tsample execution Enter search words(enter . to stop): world Success nope N

Add a comment
Know the answer?
Add Answer to:
Create a program that stores words in an STL vector and then searches the vector for...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

  • Problem #1 Create a program that performs the following functions: Uses character arrays to read a...

    Problem #1 Create a program that performs the following functions: Uses character arrays to read a user's name from standard input Tells the user how many characters are in her name Displays the user's name in uppercase Create a program that uses the strstr() function to search the string "When the going gets tough, the tough stay put! for the following occurrences (display each occurrence found to standard output): "Going" "tou" "ay put!" Build a program that uses an array...

  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • In this homework, you will create a class that stores a list of transactions and computes...

    In this homework, you will create a class that stores a list of transactions and computes the average transaction for a given month. The user must input the name of the month and the transactions into the terminal. The Transaction_List Class First you will create two C++ files called abc1234_Transaction_List.h and abc1234_Transaction_List.cpp. Below is a UML class diagram that shows the basic design of the Transaction_List class. Transaction List -month : string transactions: vector-sdouble - num transactions: int +Transaction Listim...

  • I need help please... Intro to Programming in C – small program 8 Project objective: To...

    I need help please... Intro to Programming in C – small program 8 Project objective: To compile, build, and execute an interactive program using character arrays and string.h functions **Submit source code (prog8.c) through Canvas • One source code file (unformatted text) will be submitted • The file name must match the assignment • The code should be tested and run on a Microsoft compiler before it is uploaded onto Canvas • The code must be submitted on time in...

  • Create a function john, accepts a structure of type employee and return the same structure. Ask...

    Create a function john, accepts a structure of type employee and return the same structure. Ask the user using printf and scanf to enter employee information (name, id and salary). You might use fgets for name, as scanf can not be used for strings with spaces. In main: Declare a structure of type employee Call function john and pass the structure. Display using prinf employee structure information returned from function john.

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • C++ Visual Studios - Program - Vector - Simple Create a program that utilizes VECTOR for...

    C++ Visual Studios - Program - Vector - Simple Create a program that utilizes VECTOR for the following topic. Topic: Carl's Cab Stand needs a program to keep track of their daily clients. Your program shall allow the user to enter 10 names. You will then retrieve the names, one by one, from the data structure (using the appropriate method of retrieval for each data structure) and present them on-screen so that Carl knows who to service next. Include a...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT