By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,604 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,604 IT Pros & Developers. It's quick & easy.

string extraction

P: n/a

Hi
given a coma delimited string "abc.,def.,ghj.,my name is", I need to
get the output

//output
abc.
def.
jhj.
my name is
here is how I did it but it is messy. I hope someone came up with a
cleaner version.

thank you

short Conn_task::print_smi(const string& s){
short itm = 0;
int from =0;
int sz = 0;
while( from <= s.rfind(",") ){
if( sz = s.find(",", from) )
cout << "[" << itm << "] " << s.substr(from, sz-from) << endl;
from =sz+1;
++itm;
}
cout << itm << s.substr(from, s.length()-from) << endl;
}
Nov 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

Gary Wessle wrote:
Hi
given a coma delimited string "abc.,def.,ghj.,my name is", I need to
get the output

//output
abc.
def.
jhj.
my name is
boost::tokenizer is ideal for this.
Look at the boost library, its almost part of the C++ standard and has
all the most important reinvented wheels.

Regards
Vivek

Nov 14 '06 #2

P: n/a
Here are two versions. First one is slightly modified your version, the
second is with iterato. You can use any of them which you like:

#include <string>
#include <iostream>

short SplitString(const std::string& s, char split_char = ',')
{
short itm = 0;
std::string::size_type from = 0;

for (;;)
{
std::string::size_type pos = s.find(split_char, from);
std::cout << "[" << itm << "] ";

if (pos == std::string::npos)
{
std::cout << s.substr(from) << "\n";
return itm;
}

std::cout << s.substr(from, pos - from) << "\n";
from = pos + 1;
++itm;
}
}

short SplitString2(const std::string& s, char split_char = ',')
{
short itm = 0;

std::cout << "[0] ";
for (std::string::const_iterator pos = s.begin(); pos != s.end();
++pos)
{
if (*pos == split_char)
std::cout << "\n[" << ++itm << "] ";
else
std::cout << *pos;
}

std::cout << "\n";
return itm;
}

int main()
{
std::string s("abc.,def.,ghj.,my name is");

std::cout << SplitString(s) << "\n";
std::cout << "---------\n";
std::cout << SplitString2(s) << "\n";
}

Nov 14 '06 #3

P: n/a
Why not use strtok from string.h? This example shows exactly what you
want to achieve:
http://www.cplusplus.com/ref/cstring/strtok.html

Best reguards,
Dragan

On Nov 14, 11:35 am, Gary Wessle <phd...@yahoo.comwrote:
Hi
given a coma delimited string "abc.,def.,ghj.,my name is", I need to
get the output

//output
abc.
def.
jhj.
my name is

here is how I did it but it is messy. I hope someone came up with a
cleaner version.

thank you

short Conn_task::print_smi(const string& s){
short itm = 0;
int from =0;
int sz = 0;
while( from <= s.rfind(",") ){
if( sz = s.find(",", from) )
cout << "[" << itm << "] " << s.substr(from, sz-from) << endl;
from =sz+1;
++itm;
}
cout << itm << s.substr(from, s.length()-from) << endl;
}
Nov 14 '06 #4

P: n/a


On Nov 14, 1:59 pm, "ondra.holub" <ondra.ho...@post.czwrote:
Here are two versions. First one is slightly modified your version, the
second is with iterato. You can use any of them which you like:

#include <string>
#include <iostream>

short SplitString(const std::string& s, char split_char = ',')
{
short itm = 0;
std::string::size_type from = 0;

for (;;)
{
std::string::size_type pos = s.find(split_char, from);
std::cout << "[" << itm << "] ";

if (pos == std::string::npos)
{
std::cout << s.substr(from) << "\n";
return itm;
}

std::cout << s.substr(from, pos - from) << "\n";
from = pos + 1;
++itm;
}

}short SplitString2(const std::string& s, char split_char = ',')
{
short itm = 0;

std::cout << "[0] ";
for (std::string::const_iterator pos = s.begin(); pos != s.end();
++pos)
{
if (*pos == split_char)
std::cout << "\n[" << ++itm << "] ";
else
std::cout << *pos;
}

std::cout << "\n";
return itm;

}int main()
{
std::string s("abc.,def.,ghj.,my name is");

std::cout << SplitString(s) << "\n";
std::cout << "---------\n";
std::cout << SplitString2(s) << "\n";

}- Hide quoted text -- Show quoted text -


is it not possible to replace comas in the original string with zeros
and then sending it to 'cout'?

Nov 14 '06 #5

P: n/a
Yes, it is definitely possible (I would suggest to replace it with '\n'
character to get required output) as long as you can modify (and
destroy) content of string. Then it is simillar to use of strtok.

Nov 14 '06 #6

P: n/a

Gary Wessle wrote in message ...
>
Hi
given a coma delimited string "abc.,def.,ghj.,my name is", I need to
get the output

//output
abc.
def.
jhj.
my name is

here is how I did it but it is messy. I hope someone came up with a
cleaner version.
thank you

short Conn_task::print_smi(const string& s){
short itm = 0;
int from =0;
int sz = 0;
while( from <= s.rfind(",") ){
if( sz = s.find(",", from) )
cout << "[" << itm << "] " << s.substr(from, sz-from) << endl;
from =sz+1;
++itm;
}
cout << itm << s.substr(from, s.length()-from) << endl;
}
#include <iostream>
#include <ostream>
#include <sstream>

void SplitPrint( std::istream &in, std::ostream &out){
std::string line;
while( std::getline( in, line, ',' ) ){
out << line << std::endl;
}
return;
} // SplitPrint(istream&, ostream&)

int main() // or function
{
std::cout<<"--- SplitPrint test"<<std::endl;
// std::string TheLine( "abc.,def.,ghj.,my name is" );
// std::istringstream sis( TheLine );
// SplitPrint( sis, std::cout );
// std::ofstream sofs("MyFile.txt");
// SplitPrint( sis, sofs );

std::istringstream sis( "abc.,def.,ghj.,my name is" );
std::ostringstream sos;
SplitPrint( sis, sos );
std::cout<<sos.str()<<std::endl;
std::cout<<"--- SplitPrint test end"<<std::endl;
}

/* --- SplitPrint test
abc.
def.
ghj.
my name is
--- SplitPrint test end
*/

Better?
--
Bob R
POVrookie
Nov 14 '06 #7

P: n/a

BobR wrote in message ...
>
#include <iostream>
#include <ostream>
#include <sstream>
void SplitPrint( std::istream &in, std::ostream &out){
//std::string line;
//while( std::getline(in, line, ',') ){
// out << line << std::endl;
// }

for( std::string line; std::getline( in, line, ',' ); ){
out << line << std::endl;
}

return;
} // SplitPrint(istream&, ostream&)

--
Bob R
POVrookie
Nov 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.