473,387 Members | 1,549 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,387 software developers and data experts.

Strings in C++


Well, I have an char* and I need to separe it in pieces... for example I
have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in an
array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good Idea,
I'm new working with strings in c++

Thnx in advance
--
"The best way to predict the future, is to invent it"
Mar 15 '06 #1
3 1052
> Well, I have an char* and I need to separe it in pieces... for example I
have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in an
array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good Idea,
I'm new working with strings in c++


Hi,

do you want the pointers in your array to point into the oiginal string, or
do they need to point to new buffers?

to split your string is nt so difficult:
you can use std::stringstream for this:

#include <sstream>
#include <iostream>

....

char str[] = "hello world";
std::stringstream test(str);
string str1;
string str2;
test >> str1;
test >> str2;
cout << str1;
cout << '\n';
cout << str2;

this will split your space separated string into substrings.

another way would be to use the std::string class and manually cut your
string in substrings.
that is more work, but you can do this if your string has a very specific
format that is not easy to
parse with stringstream.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 15 '06 #2
Hector Y. Martinez wrote:
Well, I have an char* and I need to separe it in pieces... for
example I have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in
an array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good
Idea, I'm new working with strings in c++


If you're using std::string, here's a simple string splitter (and test
program). This approach is more efficient than using std::stringstream,
with different flexibility tradeoffs. One thing it won't do is skip
consequtive delimitters (e.g. two space) - if there are consequtive
delimitters, you'll get an empty string in the output list.

#include <string>
#include <vector>
#include <iostream>

using namespace std;

void split_string(
const string& str,
char delimitter,
vector<string>& ret
)
{
string::size_type delim;
string::size_type prev_delim = 0;

while (string::npos != (delim = str.find_first_of(delimitter,prev_delim)))
{
ret.push_back(str.substr(prev_delim,delim-prev_delim));
prev_delim = delim+1;
}

// Get the tail after the last separator
ret.push_back(str.substr(prev_delim));
}

int main(int argc, char* argv[])
{
if (3 > argc)
{
cerr << "usage: split_test pattern\n";
return 1;
}

vector<string> args;
split_string(argv[1],' ',args);

cout << args.size() << " arguments:\n";
for (vector<string>::iterator it = args.begin(); it != args.end(); ++it)
cout << "'" << *it << "'\n";
}
Mar 15 '06 #3

Thanx both of you....

--
"The best way to predict the future, is to invent it"
"Carl Daniel [VC++ MVP]" wrote:
Hector Y. Martinez wrote:
Well, I have an char* and I need to separe it in pieces... for
example I have this:
"AREA_VERTEX -5.1108 -33.2878 0" and I need to separe this string in
an array of char* with this values...

[0] = "AREA_VERTEX"
[1] = "-5.1108"
[2] = "-33.287"
[3] = "0"

This values are separate by and blank space, please I need any good
Idea, I'm new working with strings in c++


If you're using std::string, here's a simple string splitter (and test
program). This approach is more efficient than using std::stringstream,
with different flexibility tradeoffs. One thing it won't do is skip
consequtive delimitters (e.g. two space) - if there are consequtive
delimitters, you'll get an empty string in the output list.

#include <string>
#include <vector>
#include <iostream>

using namespace std;

void split_string(
const string& str,
char delimitter,
vector<string>& ret
)
{
string::size_type delim;
string::size_type prev_delim = 0;

while (string::npos != (delim = str.find_first_of(delimitter,prev_delim)))
{
ret.push_back(str.substr(prev_delim,delim-prev_delim));
prev_delim = delim+1;
}

// Get the tail after the last separator
ret.push_back(str.substr(prev_delim));
}

int main(int argc, char* argv[])
{
if (3 > argc)
{
cerr << "usage: split_test pattern\n";
return 1;
}

vector<string> args;
split_string(argv[1],' ',args);

cout << args.size() << " arguments:\n";
for (vector<string>::iterator it = args.begin(); it != args.end(); ++it)
cout << "'" << *it << "'\n";
}

Mar 15 '06 #4

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
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 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.