Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. the rookie needs help again

the rookie needs help again

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Binary0110
    wrote on last edited by
    #1

    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

    D J 2 Replies Last reply
    0
    • B Binary0110

      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

      D Offline
      D Offline
      Dreamz
      wrote on last edited by
      #2

      Binary0110 wrote:

      I cant seem to get this to work and I can't use coditional operators.

      Why?

      1 Reply Last reply
      0
      • B Binary0110

        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

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        Can't you use a std::vector< std::string > to store the words one after another and then cout the strings using a reverse_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.

        B 1 Reply Last reply
        0
        • J jhwurmbach

          Can't you use a std::vector< std::string > to store the words one after another and then cout the strings using a reverse_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.

          B Offline
          B Offline
          Binary0110
          wrote on last edited by
          #4

          no can't use anything other than what's there. find and rfind. BINARY

          J 1 Reply Last reply
          0
          • B Binary0110

            no can't use anything other than what's there. find and rfind. BINARY

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #5

            Use a while loop, rfind the space, cout the word and in the next round rfind beginning at the position before the last space. If you cant even use the while 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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups