Word parsing loop

Hi, I am trying to write some code that will parse a string into words based on actual letters only. So, for example, the string "Hello, my name3 is Leah." needs to be separated into "Hello" "my" "name" "is" "Leah" (no punctuation or numbers). I am using a loop that goes through character by character and uses the function isalpha() which checks if the character is a letter, then adds it to the string and prints out each word.

This little piece of code does not work; it causes a segfault. I think I can sort of see why, but I can't figure out how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
string theLine = "Hello, my name3 is Leah.";


  for (size_t i = 0; i!=theLine.length(); i++) { //loops through characters in string

    if (isalpha(theLine[i])) { //if the character is a letter,
      string temp;
      for (size_t x = i; (isalpha(theLine[x])); x++) { //loops through the
        temp = temp + theLine[x];
      }
      cout << temp << endl;
    }
  }


I know that the problem is that it doesn't keep its place, so once it hits a word, it goes through that, then it starts back over at the second letter in the word.... if that makes sense. I am a little new to C++ so bear with me. Thanks for the help.
The first loop should be: for (size_t i = 0; i<theLine.length(); i++) // Note: < otherwise it goes out of bounds

The second loop doesn't make sense.

To accomplish it use an offset (= 0 before loop) for the beginning of a word. The begin offset will be set to i as long as there's no alpha. If alpha the word starts and the begin offset is not set to i. Then when the word ends (again no alpha) you can calculate the word length. Then the begin offset is set to i...
You can cout it like so: cout << theLine.substr(beg_offs, i - beg_offs) << endl;
Thanks, that first thing makes sense.

And the idea of an offset makes sense the way you describe it, but I don't really understand how to implement it. Could you give a little example of how you'd write it in code? I'm not trying to cheat, sorry, I'm just having a hard time understanding how to write it.

Thanks for your reply, I really appreciate it.
This is completely untested:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string theLine = "Hello, my name3 is Leah.";

  size_t word_begin = 0;
  for (size_t i = 0; i < theLine.length(); i++) { //loops through characters in string

    if (isalpha(theLine[i])) { //if the character is a letter,
      ; // do nothing
    else
    {
      int word_length = i - word_begin; // calculate word length
      if(word_length > 0) // is only > 0 if there was a isalpha recently
        cout << theLine.substr(word_begin, word_length) << endl; // show the word
      word_begin = i + 1; // word begin at the next position
    }
  }
Topic archived. No new replies allowed.