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. Splitting string into individual words

Splitting string into individual words

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasetutorialquestion
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.
  • R Offline
    R Offline
    Ramper
    wrote on last edited by
    #1

    I need to Write a function that will, given an input string containing many words, split that string into individual words. For each word, the function should output the word, its starting index in the string, and its length to the console without using stream extraction operator. #include #include using namespace std; int main() { string sval = " The quick brown fox jumps over the lazy dog"; int cnt=1; int nsep = sval.find(" "); while (nsep > 0) { cout << "Word " << cnt << "=" << sval.substr(0,nsep) << endl; cnt = cnt + 1; sval = sval.substr(nsep+1); nsep = sval.find(" "); } cout << "Word " << cnt << "=" << sval << endl; #ifdef WIN32 system("pause"); #endif return(0); } Thats where i am so far, it will just split it into words but will not work if it begins with a space or if there is more than 1 space between words. I believe i need s.find_first_not_of but not sure how to incorprate it or if thats even my problem any help?

    D 1 Reply Last reply
    0
    • R Ramper

      I need to Write a function that will, given an input string containing many words, split that string into individual words. For each word, the function should output the word, its starting index in the string, and its length to the console without using stream extraction operator. #include #include using namespace std; int main() { string sval = " The quick brown fox jumps over the lazy dog"; int cnt=1; int nsep = sval.find(" "); while (nsep > 0) { cout << "Word " << cnt << "=" << sval.substr(0,nsep) << endl; cnt = cnt + 1; sval = sval.substr(nsep+1); nsep = sval.find(" "); } cout << "Word " << cnt << "=" << sval << endl; #ifdef WIN32 system("pause"); #endif return(0); } Thats where i am so far, it will just split it into words but will not work if it begins with a space or if there is more than 1 space between words. I believe i need s.find_first_not_of but not sure how to incorprate it or if thats even my problem any help?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Ramper wrote:

      ...but will not work if it begins with a space...

      So just remove leading spaces.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      R 1 Reply Last reply
      0
      • D David Crow

        Ramper wrote:

        ...but will not work if it begins with a space...

        So just remove leading spaces.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        R Offline
        R Offline
        Ramper
        wrote on last edited by
        #3

        wish it was that easy, has to be a string with a leading space and multiple spaces between words.

        J D 2 Replies Last reply
        0
        • R Ramper

          wish it was that easy, has to be a string with a leading space and multiple spaces between words.

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #4

          But do you have to have the leading space in the word strings?

          -- Painstakingly Drawn Before a Live Audience

          1 Reply Last reply
          0
          • R Ramper

            wish it was that easy, has to be a string with a leading space and multiple spaces between words.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Ramper wrote:

            has to be a string with a leading space and multiple spaces between words.

            While it's being parsed? That's a very odd requirement indeed.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            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