Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to create substrings from a sentence string C++

Posted on 2008-10-23
10
908 Views
Last Modified: 2013-12-14
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 .?
0
Comment
Question by:putyoursoxon
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22795510
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 .
              {
               }
         }
    }
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22795521
I forgot to add

    pos1 = pos2 +1;

at end of loop.
0
 

Author Comment

by:putyoursoxon
ID: 22802736
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22803001
>>>> it includes the space and the first letter of the next word in the current word

Sorry, it should be

   pos2-pos1-1
0
 

Author Comment

by:putyoursoxon
ID: 22803062
Ok, let me write this into what I have so far and see how it goes, thanks
0
 

Author Comment

by:putyoursoxon
ID: 22804502
this also doesnt work does the same thing except it does it with the first word in a infinite loop
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 22806592
Take that
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
 
int tokenize(const string & sin, vector<string> & tokens, vector<char> & separators)
{
    string s = sin;
    s += char(0);    // add a 0 char for getting end-of-string parsing
    string seps = " ,.;/|";
    seps += char(0);    
    int pos1 = 0;
    int pos2 = 0;
    while ((pos2 = s.find_first_of(seps, pos1)) != string::npos) 
    {
         if (pos2 > pos1)
         {
              string sub = s.substr(pos1, pos2-pos1);
              tokens.push_back(sub);
              separators.push_back(s[pos2]);
        }
        pos1 = pos2+1;   // don't forget that or you'll get an infinite loop
    }
    return tokens.size();
}
 
int main()
{
       int i; 
       string s = "Here is a sentence that I want the program to use.";
       vector<string> substrings;
       vector<char>   separators;
 
       int n = tokenize(s, substrings, separators);
       for (i = 0; i < n; ++i)
       {
            char c = separators[i];
            cout << setw(3) << right << i+1 << " " << left << setw(20) <<  substrings[i] 
                 << " separated by " << int(separators[i]) << " " << separators[i] << endl;
       }
       cin >> i;
       return 0;
}

Open in new window

0
 

Author Closing Comment

by:putyoursoxon
ID: 31509452
im going to try a streamstring instead
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22807673
>>>> 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.
0
 

Author Comment

by:putyoursoxon
ID: 22808132
With streamstring can I use a terminating character like . To count as the end of a sentence?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question