hey guyss..!!
I'm having some difficulties working with strings (cstyle, object oriented strings).
as this is something new for me so I'm not exactly familiar with functions of strings aswell..
write now i have a question for which i was making a solution but the code is missing something please help me out.
Question statement.
"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."

My code.

#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

int main()
{
    string my_str;
    cout<<"Please enter your paragraph.."<<endl;
    getline (cin, my_str);
    cout<<endl<<endl;
    cout<<"please enter a word to be searched..!!"<<endl;
    char x;
    cin>>x;
    my_str.find("x");
    cout<<endl;
    cout<<"The total number of"<< x <<"is: "<<x;

    getch();
    return 0;
}

char x;
If you want the user to enter a string, shouldn't x be a string rather than a single char?

my_str.find("x");

You appear to be searching for the letter x. Shouldn't you be searching for the string that the user entered? You also need to do something with the result of this, and keep doing it until you can't find any more.

cout<<"The total number of"<< x <<"is: "<<x;
Shouldn't that second x actually be the number of times you found the string?

ok can you provide me with a proper solution ..?

No. You work it out and we critique it. We don't do your homework for you... :-(

See if you can make the changes suggested above ... then try it out and re-submit the new version for help ... if you need more help?

I would suggest also some more descriptive variable names ... names that help you see what is happening as the program flows ...

string my_str;
cout<<"Please enter your paragraph.."<<endl;
getline (cin, my_str);

could be changed to

cout << "Please enter your paragraph ... \n";
string paragraph;
getline( cin, paragraph );

And ... could use ...

cout << "Please enter a word to find : " << flush;
string word;
getline( cin, word ); 

This link is a also a good ref... to keep handy ...

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

Hint!

If you want to find all the words that repeat in a big string of words ...
you will need a loop ...

and as you keep searching for the next possible place the search word occurs

you will have to keep up-dating the position to begin on each call to find

so ...

use the form of find that allows you to pass in the start index ...
i.e the position to begin the new search in the string to be searched.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.