Chopping up a string of characters?

C

Cavello

Hi, I'm curious as to how I can input a string of characters and then
output it in sections, seperating these sections according to a
particular value entered, for example a period. Say if I enter
'today.is.friday', I want the screen to output:

today
is
friday

How is this done?
 
K

Karl Heinz Buchegger

Cavello said:
Hi, I'm curious as to how I can input a string of characters and then
output it in sections, seperating these sections according to a
particular value entered, for example a period. Say if I enter
'today.is.friday', I want the screen to output:

today
is
friday

How is this done?

What the program looks like is dependent on eg. how you store
that string.
But other then that: In which way did you come up with your example
output when *you* worked on that input. (One can only write a program
if one knows how to solve the very same problem without a computer, just
with paper and pencil).

I would do (pseudocode)

temp = original string
as long as I can find a '.' in temp {
output everything in temp, from left to where the '.' was found
snip from temp everything left to and including the '.'
}
output temp if it is non empty

With which part of that pseudocode do you have problems?
 
D

Donovan Rebbechi

Hi, I'm curious as to how I can input a string of characters and then
output it in sections, seperating these sections according to a
particular value entered, for example a period. Say if I enter
'today.is.friday', I want the screen to output:

today
is
friday

How is this done?

The simplest way would be to use the getline() function and set the optionl
field separator argument to "."

std::istringstream in(mystring);

while (std::getline(in, mystring, '.'))
{
stringlist.push_back(mystring);
};

Cheers,
 
D

Default User

Cavello said:
Hi, I'm curious as to how I can input a string of characters and then
output it in sections, seperating these sections according to a
particular value entered, for example a period. Say if I enter
'today.is.friday', I want the screen to output:

today
is
friday

How is this done?


This doesn't sound like homework, so I'll give you one I wrote. If it is
homework, then it's your loss not figuring it out on your own.



#include <vector>
#include <string>

// breaks apart a string into substrings separated by a character string
// does not use a strtok() style list of separator characters
// returns a vector of std::strings

std::vector<std::string> Explode (const std::string &inString,
const std::string &separator)
{
std::vector<std::string> returnVector;
std::string::size_type start = 0;
std::string::size_type end = 0;

while ((end=inString.find (separator, start)) != std::string::npos)
{
returnVector.push_back (inString.substr (start, end-start));
start = end+separator.size();
}

returnVector.push_back (inString.substr (start));

return returnVector;
}




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top