Link to home
Start Free TrialLog in
Avatar of putyoursoxon
putyoursoxon

asked on

How to create substrings from a sentence string C++

Ok this is what I am trying to do, in C++ language, I can create a string, what I want to go is take a sentence say "Here is a sentence that I want the program to use." How do I get the program to break each word into a substring so I can count the vowels in a loop, and then know to stop the loop when it gets to the .?
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

You could use STL for example

#include <string>
using namespace std;

    ....

    string s = "Here is a sentence that I want the program to use.";
    s += ' ';   // add a space for easier parsing
    int pos1 = 0;
    int pos2 = 0;
    while ((pos2 = s.find_first_of(" ,.;/|", pos1)) != string::npos)
    {
         if (pos2 > pos1)
         {
              string sub = s.substr(pos1+1, pos2-pos1+1);
              // here you could store the found token to a vector, e.g by v.push_back(sub);
              if (s[pos2] == '.')   // ok we found the .
              {
               }
         }
    }
I forgot to add

    pos1 = pos2 +1;

at end of loop.
Avatar of putyoursoxon
putyoursoxon

ASKER

I tried this, but this will not work correctly it includes the space and the first letter of the next word in the current word
>>>> it includes the space and the first letter of the next word in the current word

Sorry, it should be

   pos2-pos1-1
Ok, let me write this into what I have so far and see how it goes, thanks
this also doesnt work does the same thing except it does it with the first word in a infinite loop
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
im going to try a streamstring instead
>>>> im going to try a streamstring instead
The problem with streaming is that you could go for one delimiter only. In your case it is a space. But then, the fulstop at end must be parsed separately.
With streamstring can I use a terminating character like . To count as the end of a sentence?