| | |
help on string
![]() |
•
•
Join Date: Nov 2007
Posts: 250
Reputation:
Solved Threads: 1
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.
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)
# include <iostream> # include <string> using namespace std; int indexCounter(string sentence, int startingIndex); int main() { string line; int startIndex=0; int endIndex=0; int size= line.size(); cout<<"Please enter a sentence."<<endl; getline(cin,line); return 0; } int indexCounter(string sentence, int startIndex, int& endIndex) { for(int i=startingIndex; i<size; i++) { if(sentence[i]==' ') { return i; //white space index } endIndex=i; } }
•
•
Join Date: Jan 2008
Posts: 4,005
Reputation:
Solved Threads: 518
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:
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).
C++ Syntax (Toggle Plain Text)
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.
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Need help.
- Next Thread: Help filling list with alphabet
Views: 831 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count data delete desktop display dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int integer lazy library linker list loop loops map math matrix member memory network newbie news number object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock






