help on string

Reply

Join Date: Nov 2007
Posts: 250
Reputation: k2k is an unknown quantity at this point 
Solved Threads: 1
k2k k2k is offline Offline
Posting Whiz in Training

help on string

 
0
  #1
Feb 13th, 2008
I need to let the user input a line of words and make all the 4 letter words in the sentence "love"....

example:
cin>> I hate programming

output --> I love programming


does anybody know if there is any library, or string member functions would help doing this task?

below is so far what i have, i have a function to return the index of the space... I don't know what else i can do.
  1.  
  2. # include <iostream>
  3. # include <string>
  4. using namespace std;
  5. int indexCounter(string sentence, int startingIndex);
  6.  
  7. int main()
  8. {
  9. string line;
  10. int startIndex=0;
  11. int endIndex=0;
  12. int size= line.size();
  13.  
  14. cout<<"Please enter a sentence."<<endl;
  15. getline(cin,line);
  16.  
  17.  
  18.  
  19.  
  20. return 0;
  21. }
  22.  
  23.  
  24.  
  25. int indexCounter(string sentence, int startIndex, int& endIndex)
  26. {
  27. for(int i=startingIndex; i<size; i++)
  28. {
  29. if(sentence[i]==' ')
  30. {
  31. return i; //white space index
  32. }
  33. endIndex=i;
  34.  
  35. }
  36. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4,005
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 518
Featured Poster
VernonDozier VernonDozier is offline Offline
Industrious Poster

Re: help on string

 
0
  #2
Feb 13th, 2008
How about if you create a function that takes a large string (i.e. the user inputted sentence string named "line") and returns a vector of smaller strings? Each string in the vector would be a word. Something like this:

  1. vector <string> ParseIntoWords (string sentence);

So in your example, you pass this function the string "I hate programming" and it returns a vector of three strings:

I
hate
programming

The function you have could help parse out the separate words, as could the function "isspace" from the cctype library:
http://www.cplusplus.com/reference/c...e/isspace.html

Also potentially useful could be this function:
http://www.cplusplus.com/reference/s...ng/substr.html

You could then go through this vector of strings and check to see whether a string had four letters. If so, change the string. You end up with a vector like this:
I
love
programming

Concatenate the above vector of strings and you have your revised string (except from the period. Also you may have to put the spaces back in).
Last edited by VernonDozier; Feb 13th, 2008 at 3:48 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4,327
Reputation: WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future 
Solved Threads: 414
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Industrious Poster

Re: help on string

 
0
  #3
Feb 13th, 2008
Use .find() to find the spaces. If you find 2 spaces with 4 characters between them, use .replace() to replace the 4 characters.

And remember, the end of the sentence may not end in a space, but it may end in a 4-letter word.
Last edited by WaltP; Feb 13th, 2008 at 6:46 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 831 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC