943,845 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1040
  • C++ RSS
Feb 13th, 2008
0

help on string

Expand Post »
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.
cpp Syntax (Toggle Plain Text)
  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. }
Similar Threads
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Feb 13th, 2008
0

Re: help on string

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:

C++ Syntax (Toggle Plain Text)
  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 4:48 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Feb 13th, 2008
0

Re: help on string

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 7:46 pm.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help.
Next Thread in C++ Forum Timeline: Help filling list with alphabet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC