count and position

Very new to c++ I was trying to solve one common question but not sure how to proceed. Help will be really appreciated. (I am trying to solve without using inbuilt functions!!)

Question is something like below:

By taking string user input we have to display how many times the string(word) is repeated in the sentence and at which position?

For example: Output should be something like below:

Enter ur string : “I am a sample sample program”
I -> 1time-> 0th position
am->1 time->2nd position
a-> 1 time -> 5th position
sample -> 2 time ->7th position
sample -> 2 time ->14th position
program -> 1 time -> 22th position
Last edited on
Just read up on c++ string class.
http://www.cplusplus.com/reference/string/string/

In particular, read up on the find function.
http://www.cplusplus.com/reference/string/string/find/
Hey pnoid I read what you suggested but is there any way without using inbuilt functions !!
Last edited on
There's tons of ways to do it.
If you can't use the string class you can do it with char arrays.
If I had to do it that way I might create a struct to store information about each substring.
Then loop through the sentence to get the substrings, which I would do by adding letters to a char array until I hit a space or punctuation, and obviously record their positions in the struct at the same time.
Then compare each substring to every other substring and increment their count if they are identical.
Then display the results.

Hi sr123.
This program will need use of strings. I will try to solve this problem and show it to your. Just wait a little.
Topic archived. No new replies allowed.