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

Chopping up a string of characters?

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?
Jul 22 '05 #1
3 1846
Cavello wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Cavello wrote:

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
Jul 22 '05 #3
In article <63**************************@posting.google.com >, Cavello wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #4

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

Similar topics

2
by: Dag Eilertsen | last post by:
Hi! I'm wondering if there is an easy way to divide a string into two parts (and save them into two varibles). The things is that I'm trying to chop a file name in two with the name in one...
3
by: Madhusudan Singh | last post by:
Hi I am a newbie to python and am using it to interface some of my lab equipment. How does one get rid of spaces at both ends of a string ? A little like the trim() intrinsic in fortran 95. ...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
0
by: Paul Hobbs | last post by:
Hi, I wanted to create a function which forced a user to enter no more than a specified number of characters. The code for this is below. The idea was simple, if a user is only meant to enter 10...
7
by: Paul Furman | last post by:
mysql_real_escape_string() is apparently chopping off anything that follows a quote when I grab the data & put it in a form for editing. Sorry if I'm not explaining this properly, I'm pretty...
3
by: djm | last post by:
hello everyone. ive a string with a blank space at the start for eg: string str=" c++ rocks!"; how can i chop of that blank space of the string and display? thanks
1
by: Naveenv2002 | last post by:
I am using granados parser for connecting to the telnet and get the data from it. However, every thing appears to be going smooth. But for some reason when I try to write to a string or streamwriter,...
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:
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,...
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...
0
tracyyun
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...

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.