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;
} 7 1931
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
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";
}
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;
}
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'?
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Xah Lee |
last post by:
# strings can be joined by +.
print "this" + " that"
# string can be multiplied
print "this" *5
# substring extraction is done by appending a bracket
# with begin and ending index
a="this...
|
by: ma740988 |
last post by:
The string object value_f doesn't produce the right output. At issue,
- I suspect - is the conversion from string to int with istringstream.
An alternate approach? Thanks in advance
#include...
|
by: Adam Parkin |
last post by:
Hello all, I'm trying to write a function which given a std::string
parses the string by breaking the sentance up by whitespace (\t, ' ',
\n) and returns the result as a vector of strings. Here's...
|
by: Jason Huang |
last post by:
Hi,
Would someone show me how to do the data extraction to Excel in ASP.Net
using C# web form?
I am not familiar with VB, so I am asking someone to help me out!
Any help will be appreciated.
...
|
by: James Lehman |
last post by:
Hello.
I want to write a program that reads AutoCAD shape (font) files. They are
written with the convention that hexadecimal values have a leading zero and
decimal values do not. All numbers...
|
by: TheSteph |
last post by:
Hi,
I'm new to Regex..
Could someone show me how I can extract substring enclosed in ?
Example :
|
by: dec01louis |
last post by:
Hi all, actually i'm now doing something on license plate recognition system for my project. The first step would be the license plate extraction algorithm which means it is needed to extract a...
|
by: kellysgirl |
last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written.
The instructions I was given are as follows
In this case, you will create...
|
by: Jacek Dziedzic |
last post by:
Hi!
I need a routine like:
std::string nth_word(const std::string &s, unsigned int n) {
// return n-th word from the string, n is 0-based
// if 's' contains too few words, return ""
//...
|
by: Taras_96 |
last post by:
Hi all,
Jesse Liberty writes:
"cin.get() >>myVarOne >myVarTwo; // illegal
The return value of (cin.get() >myVarOne) is an integer, not an
iostream object."
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |