views:

318

answers:

3

I have problem in string maniputation with c++. The Rule : if the same 'word' is repeated from sentences or paragraph i want it to become an integer. Please help me ?!

example

input : we prefer questions that can be answered, not just we discussed that.

output: 1 prefer questions 2 can be answered, not just 1 discussed 2.

1 we 2 that

thanks before...

+4  A: 

This type of problem is usually much easier to solve if you use an associative array to keep track of the words you have already seen. Try using an STL map for storing words you have seen already. It will take some work to get your logic set up correctly, but a map will definitely help with what you are trying to do.

A. Levy
thanks for the help
+4  A: 

This is the approach I would take (algorithms only, since it's homework).

  1. Create a data structure mapping words to counts.
  2. Process the text one word at a time.
    • if it's a new word, add it to the data structure and set its count to 1.
    • if it's an existing one, just increment the count.
  3. Once all words are processed, go through each word in the data structure, giving a unique integer to those with a count greater than one.
  4. Create a new text string, empty to start with then process the text word-by-word again.
    • if the word has a count of one, append that word to the new string.
    • if the count is greater than one, append the unique integer.
paxdiablo
thanks for helping
This is the one that holds the key for the novice programmer: the solution involves two consecutive passes over the data. Consider carefully what you need to do in pass 1, and what you then do in pass 2.
John Pirie
+1  A: 

Parsing:

   For each word in the string
          Check whether the word exists in map<WORD,Counter>
          if the WORD is new the insert into the map with counter =0
          otherwise increment the counter associated with word.

Output:(create new sentence)

For each word in the string
      Lookup into the vector for counter value
      if counter ==0 then insert WORD as it is
      otherwise convert the counter to string and insert
aJ
thanks for helping... :-)