Separate sentences into words with string and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xephia
    New Member
    • Oct 2006
    • 4

    Separate sentences into words with string and arrays

    Hey all, I am resorting to posting this question after getting absolutely nowhere in four days of searching (except for maybe negative steps, got mad at my laptop and broke my delete key :( )


    Anyway, my problem is that I need to separate sentences into words. I figure that the only logical way of doing this would be to use an array (and I need to for other parts of this program which I have already made)

    However, I can't figure out how to gut the sentence into spaces using a loop that will pick up on the space, and put the word into one element and then start again and put the next into the next element.

    So far i have tried this code, but i doubt its helpful.

    Code:
    do
    {
        for(i=0;int(word1.length());i++)
      {
        nextchar=word1.at(i);//get char
    
    if(nextchar!=' ')
      {
        for(i=0;i<MAX;i++)
              nextchar=word[i];
    }
    }
    }
    
    while(nextchar!='.');
    Anyway, I know theres probably some errors in the formatting and spelling and stuff in that, but thats my latest concoction of how to possibly do it, I know its wrong :(

    Please help, I will pray for whoever helps me tonight before i go to sleep ;)

    THANKS in advanced for ANY help... but if you can give me a snippet of code it would be greatly appreciated.
  • xephia
    New Member
    • Oct 2006
    • 4

    #2
    Originally posted by xephia
    Hey all, I am resorting to posting this question after getting absolutely nowhere in four days of searching (except for maybe negative steps, got mad at my laptop and broke my delete key :( )


    Anyway, my problem is that I need to separate sentences into words. I figure that the only logical way of doing this would be to use an array (and I need to for other parts of this program which I have already made)

    However, I can't figure out how to gut the sentence into spaces using a loop that will pick up on the space, and put the word into one element and then start again and put the next into the next element.

    So far i have tried this code, but i doubt its helpful.

    Code:
    do
    {
        for(i=0;int(word1.length());i++)
      {
        nextchar=word1.at(i);//get char
    
    if(nextchar!=' ')
      {
        for(i=0;i<MAX;i++)
              nextchar=word[i];
    }
    }
    }
    
    while(nextchar!='.');
    Anyway, I know theres probably some errors in the formatting and spelling and stuff in that, but thats my latest concoction of how to possibly do it, I know its wrong :(

    Please help, I will pray for whoever helps me tonight before i go to sleep ;)

    THANKS in advanced for ANY help... but if you can give me a snippet of code it would be greatly appreciated.

    P.S

    this is C++

    Comment

    • Fir3Bat
      New Member
      • Oct 2006
      • 19

      #3
      wats ur point xephia?

      Comment

      • Fir3Bat
        New Member
        • Oct 2006
        • 19

        #4
        o nvm i didn't know you responded first lol

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Once you have the entire string the user enters, doesn't it make sense that a word would lie between two spaces? You can consistantly search for a space in the whole sentence, and then set word equal to the proper substring of the sentence. You might need a function that will clear the spaces from in front of a word, and you will call this function before every iteration of your loop - which should be a while loop, with the condition that the sentence's length is not 0.

          If you still have trouble, go ahead and ask - I recently did a similar program and will have access to the code Monday-Friday (I have it saved at school).

          Comment

          • xephia
            New Member
            • Oct 2006
            • 4

            #6
            Originally posted by Ganon11
            Once you have the entire string the user enters, doesn't it make sense that a word would lie between two spaces? You can consistantly search for a space in the whole sentence, and then set word equal to the proper substring of the sentence. You might need a function that will clear the spaces from in front of a word, and you will call this function before every iteration of your loop - which should be a while loop, with the condition that the sentence's length is not 0.

            If you still have trouble, go ahead and ask - I recently did a similar program and will have access to the code Monday-Friday (I have it saved at school).

            If you could give me that code it would be great for a starting point


            I'm still stuck, this is soo frusturating

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              I'll give you the first half of my code: the main() and the function prototypes. But I'm leaving it up to you to write the function bodies. Otherwise, you wouldn't learn anything.

              Code:
              /* Chapter 8 Worksheet Program 2
              Independent Study, Michael Stark */
              
              #include<iostream>
              #include<string>
              using namespace std;
              
              void extractFirstWord(string& sentence, string& word);
              void processBlanks(string& sentence);
              
              int main() {
                  string sentence, word;
                  cout << "Input a sentence: ";
                  getline(cin, sentence);
                  
                  while (sentence != "") {
                        processBlanks(sentence); // removes all blanks from the front of sentence
                        if (sentence.length() > 0) { // removing blanks may have made sentence null - cannot extract from a null string
                           extractFirstWord(sentence, word); // gets first word from sentence and puts into word
                           cout << word << endl; // output one word at a time
                        }
                  }
                  
                  system("PAUSE");
                  return 0;
              }
              
              void extractFirstWord(string& sentence, string& word);
              // extractFirstWord removes the substring of sentence 
              // from the beginning to the first space from sentence 
              // and stores the same string into word. sentence is
              // shortened accordingly.
              // Postcondition: sentence is shortened, and word
              //                is appropriately the first word in
              //                sentence.
              
              void processBlanks(string& sentence);
              // processBlanks will remove all of the spaces in front
              // of sentence.
              // Postcondition: sentence has no spaces in front of
              //                the first word.

              Comment

              • xephia
                New Member
                • Oct 2006
                • 4

                #8
                Thanksss!! that was alot of help!

                Comment

                Working...