473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Required string parser

Hello,

I am writing one application in which I am getting data as a string "
10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it
by pipe '|' and get integer outputs in some array or other data
structure.....

do anybody worked in the same functionality, then please let me know.

Thanks,
Rushik.

Oct 4 '06 #1
8 1870


? "rushik" <ru*************@gmail.com?????? ??? ??????
news:11**********************@c28g2000cwb.googlegr oups.com...
Hello,

I am writing one application in which I am getting data as a string "
10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it
by pipe '|' and get integer outputs in some array or other data
structure.....

do anybody worked in the same functionality, then please let me know.

Thanks,
Rushik.
You can use the strtok function:
http://www.cplusplus.com/ref/cstring/strtok.html

Be careful though, strtok works with char *.
--
Serafeim
Oct 4 '06 #2

Papastefanos Serafeim wrote:
? "rushik" <ru*************@gmail.com?????? ??? ??????
news:11**********************@c28g2000cwb.googlegr oups.com...
Hello,

I am writing one application in which I am getting data as a string "
10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it
by pipe '|' and get integer outputs in some array or other data
structure.....

do anybody worked in the same functionality, then please let me know.

Thanks,
Rushik.
Hi,

Seems simple. My suggestion...

Iterate till length of string
{
Iterate till '|' found
{
extract char at ctr and concatenate char to std::string
increment ctr
}
increment ctr
if(length of string 0)
{
convert temp string into number using atoi() and store in int vector
reset std::string
}
}

Convert the pseudo code into C++ :) If it is production code, then you
will obviously put in more checks.

Regards,
Abhishek Srivastava

Oct 4 '06 #3

rushik wrote:
Hello,

I am writing one application in which I am getting data as a string "
10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it
by pipe '|' and get integer outputs in some array or other data
structure.....

do anybody worked in the same functionality, then please let me know.

Thanks,
Rushik.
If however you prefer doing it yourself, the std::string is well
capable of pumping out a parser. Keep in mind that you should use
std::string::size_type to hold those values returned by find_first_of
-like member functions. You can then std::istringstream the substrings
to generate the numbers. The std::string class includes a panoply of
member functions and ctors that will let you substring at construction
time or using assign(...), etc.

Here is one nasty example that uses 2 functions, one to populate a
container of relevant substrings and another templated function to
convert the substrings into a container of numbers. It'll work with
any seperator including spaces, comma, colon, etc or any combination of
these. The error checking is rudimentary at best (you need to supply
the correct seperator).

Take note that t_track is used to prevent find_first_of from rolling up
to std::string::npos ( a costly operation on some implementations).
Also, there is most likely better ways of doing this.
check out boost's tokenizer, for example.

#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <sstream>
#include <stdexcept>

void parser( std::vector<std::string>& r_v,
const std::string& s,
const std::string& r_sep )
{
std::string::size_type t_track = s.find_last_of(r_sep);
if ( t_track == std::string::npos )
{
throw std::exception(); // no sep found
}
std::string s_temp(s); // decapitating string
while ( t_track != std::string::npos )
{
std::string::size_type t_size = s_temp.find_first_of(r_sep);
std::string s_add(s_temp, 0, t_size);
std::cout << "s_add = " << s_add << std::endl;
r_v.push_back(s_add);
t_track = t_track - s_add.size() - r_sep.size(); // npos?
s_temp.assign(s_temp, t_size + r_sep.size(), s_temp.size());
std::cout << "s_temp = " << s_temp << std::endl;
}
r_v.push_back(s_temp); // push last one
}

template< typename T >
void converter( std::vector<std::string>& r_vs,
std::vector<T>& r_vt )
{
typedef std::vector<std::string>::iterator VIter;
VIter iter = r_vs.begin();
for (iter; iter != r_vs.end(); ++iter)
{
T t;
std::istringstream iss(*iter);
iss >t;
r_vt.push_back(t);
}
}

int main()
{
std::string s("10 | 20 | 30 | 40 | 50");
// std::string s("-11 20 -3 2 4 3 1 -6");
std::vector<std::stringvs;

try
{
parser(vs, s, " | ");
std::vector<intvn;
converter<int>(vs, vn);
for (size_t i = 0; i < vn.size(); ++i)
{
std::cout << "vn[ " << i << "] = ";
std::cout << vn.at(i) << std::endl;
}
}
catch ( const std::exception& e )
{
std::cout << "Error !" << std::endl;
}

return 0;
}

/*
s_add = 10
s_temp = 20 | 30 | 40 | 50
s_add = 20
s_temp = 30 | 40 | 50
s_add = 30
s_temp = 40 | 50
s_add = 40
s_temp = 50
vn[ 0] = 10
vn[ 1] = 20
vn[ 2] = 30
vn[ 3] = 40
vn[ 4] = 50
*/

Oct 4 '06 #4
On 4 Oct 2006 09:49:27 -0700, "Salt_Peter" <pj*****@yahoo.comwrote:
>
template< typename T >
void converter( std::vector<std::string>& r_vs,
std::vector<T>& r_vt )
{
typedef std::vector<std::string>::iterator VIter;
VIter iter = r_vs.begin();
for (iter; iter != r_vs.end(); ++iter)
{
T t;
std::istringstream iss(*iter);
iss >t;
r_vt.push_back(t);
}
}

int main()
{
std::string s("10 | 20 | 30 | 40 | 50");
// std::string s("-11 20 -3 2 4 3 1 -6");
std::vector<std::stringvs;

try
{
parser(vs, s, " | ");
std::vector<intvn;
converter<int>(vs, vn);
for (size_t i = 0; i < vn.size(); ++i)
{
std::cout << "vn[ " << i << "] = ";
std::cout << vn.at(i) << std::endl;
}
}
catch ( const std::exception& e )
{
std::cout << "Error !" std::endl;
}

return 0;
}
Your try/catch blocks seem to indicate sufficient error treatment. But
the most problematic part of the program, the conversion between sting
and int (with inefficient stringstream), omits any error handling.

Best wishes,
Roland Pibinger
Oct 4 '06 #5

rushik wrote:
Hello,

I am writing one application in which I am getting data as a string "
10 | 20 | 30 | 40 | 50" now my aim is to parse those string, split it
by pipe '|' and get integer outputs in some array or other data
structure.....

do anybody worked in the same functionality, then please let me know.

Thanks,
Rushik.
std::string data="10 | 20 | 30 | 40 | 50";
std::stringstream sstr(data); // include sstream
int i1, i2, i3,i4,i5;
char c;
sstr >i1>>c>>i2>>c>>i3>>c>>i4>>c>>i5;

or like this
int i;
char c;
std::vector<intvec;
while(sstr.rdbuf()->in_avail())
{
sstr>>i>>c;
vec.push_back(i) ;
}

There are many possibilities.
strtok and sscanf are the worst of them.

Best Regards,
Valentin Heinitz

http://heinitz-it.de

Oct 4 '06 #6

Roland Pibinger wrote:
On 4 Oct 2006 09:49:27 -0700, "Salt_Peter" <pj*****@yahoo.comwrote:

template< typename T >
void converter( std::vector<std::string>& r_vs,
std::vector<T>& r_vt )
{
typedef std::vector<std::string>::iterator VIter;
VIter iter = r_vs.begin();
for (iter; iter != r_vs.end(); ++iter)
{
T t;
std::istringstream iss(*iter);
iss >t;
r_vt.push_back(t);
}
}

int main()
{
std::string s("10 | 20 | 30 | 40 | 50");
// std::string s("-11 20 -3 2 4 3 1 -6");
std::vector<std::stringvs;

try
{
parser(vs, s, " | ");
std::vector<intvn;
converter<int>(vs, vn);
for (size_t i = 0; i < vn.size(); ++i)
{
std::cout << "vn[ " << i << "] = ";
std::cout << vn.at(i) << std::endl;
}
}
catch ( const std::exception& e )
{
std::cout << "Error !" std::endl;
}

return 0;
}

Your try/catch blocks seem to indicate sufficient error treatment. But
the most problematic part of the program, the conversion between sting
and int (with inefficient stringstream), omits any error handling.

Best wishes,
Roland Pibinger
I agree. i see that a simple if_conversion_test should provide a simple
solution:
http://www.parashift.com/c++-faq-lit...al-issues.html
[39.2] How do I convert a std::string to a number?

int n;
std::string s;
std::istringstream iss(s);
if ( !(i >n) ) // test
throw std::exception();
....

Have you a better proposal insofar as the std::stringstream is
concerned?
Did you mean inefficient as in the stream being regenerated over each
loop?
Or rdbuf() and clear() is what you had in mind?

Oct 5 '06 #7

Roland Pibinger wrote:
On 4 Oct 2006 09:49:27 -0700, "Salt_Peter" <pj*****@yahoo.comwrote:

template< typename T >
void converter( std::vector<std::string>& r_vs,
std::vector<T>& r_vt )
{
typedef std::vector<std::string>::iterator VIter;
VIter iter = r_vs.begin();
for (iter; iter != r_vs.end(); ++iter)
{
T t;
std::istringstream iss(*iter);
iss >t;
r_vt.push_back(t);
}
}

int main()
{
std::string s("10 | 20 | 30 | 40 | 50");
// std::string s("-11 20 -3 2 4 3 1 -6");
std::vector<std::stringvs;

try
{
parser(vs, s, " | ");
std::vector<intvn;
converter<int>(vs, vn);
for (size_t i = 0; i < vn.size(); ++i)
{
std::cout << "vn[ " << i << "] = ";
std::cout << vn.at(i) << std::endl;
}
}
catch ( const std::exception& e )
{
std::cout << "Error !" std::endl;
}

return 0;
}

Your try/catch blocks seem to indicate sufficient error treatment. But
the most problematic part of the program, the conversion between sting
and int (with inefficient stringstream), omits any error handling.

Best wishes,
Roland Pibinger
I agree. i see that a simple if_conversion_test should provide a simple
solution:
http://www.parashift.com/c++-faq-lit...al-issues.html
[39.2] How do I convert a std::string to a number?

int n;
std::string s;
std::istringstream iss(s);
if ( !(iss >n) ) // test
throw std::exception();
....

Have you a better proposal insofar as the std::stringstream is
concerned?
Did you mean inefficient as in the stream being regenerated over each
loop?
Or rdbuf() and clear() is what you had in mind?

Oct 5 '06 #8
On 4 Oct 2006 19:49:56 -0700, "Salt_Peter" <...@yahoo.comwrote:
>i see that a simple if_conversion_test should provide a simple
solution:
http://www.parashift.com/c++-faq-lit...al-issues.html
[39.2] How do I convert a std::string to a number?

int n;
std::string s;
std::istringstream iss(s);
if ( !(i >n) ) // test
throw std::exception();
...

Have you a better proposal insofar as the std::stringstream is
concerned?
Did you mean inefficient as in the stream being regenerated over each
loop?
At least move the stringstream out of the loop. Alternatively you may
consider to encapsulate strtol or similar functions (but those
functions are a little tricky).

Best wishes,
Roland Pibinger

Oct 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matthieu Gaillet | last post by:
Hello everyone, I try to launch a simple aspx (webmatrix) page onto an W2k / IIS5.0 / .net framework 1.1 but still get this error page : Server Error in '/pagaille' Application....
6
by: yzzzzz | last post by:
Hi, In which cases is the <?xml version="1.0" encoding="UTF-8"?> processing instruction required at the beginning of an XML document, for the document to be valid? e.g. does it depend on the...
4
by: Sridhar | last post by:
HI All, Currently writing a small program and here is my xml file and program. --------------------------------------------------------------------------------- <?xml version="1.0"...
5
by: Graham | last post by:
I have created a custom MembershipProvider called "LassieMembershipProvider" that derives from "MembershipProvider". This providor is located in a Businesslogic layer dll called...
4
by: sturnfie | last post by:
Hey all, I recently came across the xml.sax libraries and am trying to use them. I am currently making a string variable, and am attempting to pass it into a parser instance as follows: def...
1
by: spardi | last post by:
I try to launch a simple aspx (webmatrix) page onto an W2k / IIS5.0 / .net framework 1.1 but still get this error page : Server Error in '/pagaille' Application....
11
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
9
nine72
by: nine72 | last post by:
Ok, so I have figured out how to parse an custom returned XML doc (actually a string that I made into a doc for testing). At this point I am attempting to integrate my parse routine into my main...
4
by: stroudg2 | last post by:
Situation: I have been tasked to provide a non-intrusive solution to allow .NET web services to grab an incoming SOAP header and process the credentials stored within in such a way that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.