the rookie needs help again
-
I'm trying to do this project that reads a four word sentence from one string and then extract each word in the string to four other strings using the spaces between the words as indexes.Then display them in reverse. I cant seem to get this to work and I can't use coditional operators. Any suggestions.Here what I have so far.
#include #include using namespace std; int main(void) { string sentence; cout << "Enter a four word sentence:"; getline(cin,sentence); cout << "Original string:"<< sentence << endl; int blank=(" "); int firstindex = sentence.find(blank); string first = sentence.substr(0,firstindex); int secondindex = sentence.find(blank,firstindex + 1); string second = sentence.substr(blank + 1,secondindex); int forthindex = sentence.rfind(blank); string forth= sentence.substr(sentence.length,blank); int thirdindex= sentence.rfind(blank,forthindex - 1) string third= sentence.substr(thirdindex,sentence.length - 1); cout << "Reversed string:" << forth+" "+third+" "+second+" "+first<< endl;
BINARY -
I'm trying to do this project that reads a four word sentence from one string and then extract each word in the string to four other strings using the spaces between the words as indexes.Then display them in reverse. I cant seem to get this to work and I can't use coditional operators. Any suggestions.Here what I have so far.
#include #include using namespace std; int main(void) { string sentence; cout << "Enter a four word sentence:"; getline(cin,sentence); cout << "Original string:"<< sentence << endl; int blank=(" "); int firstindex = sentence.find(blank); string first = sentence.substr(0,firstindex); int secondindex = sentence.find(blank,firstindex + 1); string second = sentence.substr(blank + 1,secondindex); int forthindex = sentence.rfind(blank); string forth= sentence.substr(sentence.length,blank); int thirdindex= sentence.rfind(blank,forthindex - 1) string third= sentence.substr(thirdindex,sentence.length - 1); cout << "Reversed string:" << forth+" "+third+" "+second+" "+first<< endl;
BINARY -
I'm trying to do this project that reads a four word sentence from one string and then extract each word in the string to four other strings using the spaces between the words as indexes.Then display them in reverse. I cant seem to get this to work and I can't use coditional operators. Any suggestions.Here what I have so far.
#include #include using namespace std; int main(void) { string sentence; cout << "Enter a four word sentence:"; getline(cin,sentence); cout << "Original string:"<< sentence << endl; int blank=(" "); int firstindex = sentence.find(blank); string first = sentence.substr(0,firstindex); int secondindex = sentence.find(blank,firstindex + 1); string second = sentence.substr(blank + 1,secondindex); int forthindex = sentence.rfind(blank); string forth= sentence.substr(sentence.length,blank); int thirdindex= sentence.rfind(blank,forthindex - 1) string third= sentence.substr(thirdindex,sentence.length - 1); cout << "Reversed string:" << forth+" "+third+" "+second+" "+first<< endl;
BINARYCan't you use a
std::vector< std::string >
to store the words one after another and thencout
the strings using areverse_iterator
?
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.
-
Can't you use a
std::vector< std::string >
to store the words one after another and thencout
the strings using areverse_iterator
?
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.
no can't use anything other than what's there. find and rfind. BINARY
-
no can't use anything other than what's there. find and rfind. BINARY
Use a
while
loop,rfind
the space,cout
the word and in the next roundrfind
beginning at the position before the last space. If you cant even use thewhile
loop skip the lessons until you can...:-D
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.