count and position

Jan 31, 2016 at 10:02pm
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 Jan 31, 2016 at 11:23pm
Jan 31, 2016 at 11:08pm
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/
Jan 31, 2016 at 11:14pm
Hey pnoid I read what you suggested but is there any way without using inbuilt functions !!
Last edited on Jan 31, 2016 at 11:15pm
Feb 1, 2016 at 1:11am
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.

Feb 2, 2016 at 11:25am
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.