I have another question and trying to solve but did not solve this anyone who help me to solve this the Question start here......

Write a program that reads a whole paragraph (you can do that with a little common sense) from the user. Now prompt the user enters a word to be searched. Your program should read the search word and search for all the occurrences of that word in the entered text. You need to print the total number of occurrences of this word in the entered text.

Sorry, but we don't do your homework for you. Post your work (code) and we will critique it. To get you started though, read the paragraph and parse each word, placing them in a map (sorted list) with counts which you can then easily search from the user input. IE, when parsing the paragraph, if you find the word in the map, you increment the count by one. If not found you add it with a count of 1. The rest of exercise becomes trivial after that. The main thing is to determine what are delimiters for words, such as spaces, commas, periods, and such. And then to skip non-words such as elipses (...) and hyphens, etc.

Have fun!

//my code
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
    string s1;
    cout<<"\nPlease Enter Paragraph\n";
    getline(cin,s1);
    cout<<"\nPlease Enter a Word You Find\n";
    string s2;
    getline(cin,s2);
    cout<<s1.find(s2);


    getch();
    return 0;
}

Ok ... bad idea to use conio.h and getch ... if you want to use standard C++ and have portable code ...

But where is your attempt to use the C++ find a string function ... to find some string in some longer string?

Instead of using getch to keep the window open at the end ...

Keep the cin stream flushed (as you go) ... and then you can use cin.get() at the end to keep the window open.

Did you check the link?

http://www.cplusplus.com/reference/string/string/find/

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.