Tuesday, July 28, 2009

How can I give number to each word in c++?

for example for this sentence:


I am student I love c++


displays:


012034


of course for long sentences?

How can I give number to each word in c++?
looks like every word is assigned a number and is stored into an array. the method of solving this problems is to create a loop that looks through this string of letters. When it finds a space it knows that there is a word. you can generate a new string to store the word being created until it finds the space. In the problem you show,





"I" = 0


"am" = 1


"student" = 2


"love" = 3


"c++" = 4





aftter you run the loop to store each word into an array. You use another loop to print out a number for each word it finds. Therefore creating the output of: 012034
Reply:As your program collects each word, it should store it in a list in an object like so:


class numwordthing {


string s;


int w;


}





Whenever you get a word, check to see if it already in the list and use the existing number. If it is not in the list, store it with the next word number.





Thus:


My cow can carry a can of beans.


12345367





You may want to separate the numbers with a space or a comma when you print them so they don't run together.


No comments:

Post a Comment