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

Trying to tokenize a string

I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain word

char *mystring "Jane and Peter and Tom and Cindy"
char *delim = " and ";
char *token;

token = strtok(mystring, delim);

while (token !=NULL )
{
//do some work
cout << token << endl;
token = strtok(NULL,delim)
}

my output would return JePererTomCiy
I need my output to be Jane Peter Tome Cindy

How can I accomplish this output

thanks!
Jul 19 '05 #1
9 18401
"Lans" <lr******@bn.com> wrote in message
news:19*************************@posting.google.co m...
I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain word

char *mystring "Jane and Peter and Tom and Cindy"
char *delim = " and ";
char *token;

token = strtok(mystring, delim);

while (token !=NULL )
{
//do some work
cout << token << endl;
token = strtok(NULL,delim)
}

my output would return JePererTomCiy
I need my output to be Jane Peter Tome Cindy

JePererTomCiy

That output doesn't make any sense, unless you mis-typed it... <?>
Jeremy

Jul 19 '05 #2


Lans wrote:

I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain word

If you are dead set on using C-style strings instead of C++ std::string
class, then the best way is probably to use strstr(). Here's an example,
written almost completely in C (except for the bool). Note that no
precaution for overrun of buf[] is taken, it's done by inspection for
this problem.
#include <stdio.h>
#include <string.h>
int main()
{
char *mystring = "Jane and Peter and Tom and Cindy";
char *delim = " and ";
char *head;
char *tail;
char buf[80];
bool flag = true;

size_t len = strlen (delim);

head = mystring;

while (flag)
{
buf[0] = 0;

if ((tail = strstr (head, delim)) == 0)
{
strcpy (buf, head);
flag = false;
}
else
{
strncat (buf, head, tail-head);
head = tail + len;
}

puts (buf);
}

return 0;
}

Result:

Jane
Peter
Tom
Cindy

I don't recommend doing it this way, of course.


Brian Rodenborn
Jul 19 '05 #3
sw
It looks as if you misunderstood what the <<char*delim>> does
You tokenized the string with delimiting chars ' ','a','n','d',' '
to something like "J\n" "e\n" "Pe\n" later you put them into std::cout
without spaces.
Delimiters are single characters, not strings.

try something like this :

char mystring[] = "Jane and Peter and Tom and Cindy";
char *delim = " "; // only blank
char *token;

token = strtok(mystring, delim);

while (token !=NULL)
{
//compare ... (strcmp (token,"and") )
//continue loop if equal
cout << token << endl;
token = strtok(NULL,delim)
}


"Lans" <lr******@bn.com> wrote in message
news:19*************************@posting.google.co m...
I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain word

char *mystring "Jane and Peter and Tom and Cindy"
char *delim = " and ";
char *token;

token = strtok(mystring, delim);

while (token !=NULL )
{
//do some work
cout << token << endl;
token = strtok(NULL,delim)
}

my output would return JePererTomCiy
I need my output to be Jane Peter Tome Cindy

How can I accomplish this output

thanks!

Jul 19 '05 #4
Lans <lr******@bn.com> wrote in message
news:19*************************@posting.google.co m...
I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain word

char *mystring "Jane and Peter and Tom and Cindy"
char *delim = " and ";
char *token;

token = strtok(mystring, delim);

while (token !=NULL )
{
file://do some work
cout << token << endl;
token = strtok(NULL,delim)
}

my output would return JePererTomCiy
I need my output to be Jane Peter Tome Cindy

How can I accomplish this output


First, I'd use a 'std::string' object instead of
an array of characters.

The code below accomodates either a character array
or a std::string.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

typedef std::istream_iterator<std::string> istrit;
typedef std::ostream_iterator<std::string> ostrit;

std::string xfrm(const std::string& s)
{
return s == "and" ? "" : s + ' ';
}

/* void replace_delims(std::string& s) */
/* -- modifies argument 's' as follows: */
/* */
/* - Removes all occurrences of the string "and" which are */
/* delimited by whitespace and/or end-of-string */
/* */
/* - Replaces consecutive whitespace characters with a */
/* single space character */
/* */
/* - Removes leading and trailing whitespace */
void replace_delims(std::string& s)
{
std::ostringstream oss;

std::transform(istrit(std::istringstream(s)), istrit(),
ostrit(oss), xfrm);

const std::string& ref = oss.str();
s = ref.substr(0, ref.size() - !ref.empty());
}

/* void replace_delims(char* s) */
/* -- Same as 'void replace_delims(std::string&)', */
/* but operates on a 'C-style string' */
void replace_delims(char *s)
{
std::string result(s);
std::string::size_type sz(result.size());
replace_delims(result);
std::copy(result.begin(), result.begin() + sz, s);
s[sz] = 0;
}

int main()
{
char mystring[] = "Jane and Peter and Tom and Cindy";
std::cout << "Before:\n" << '#' << mystring << '#' << "\n\n";
replace_delims(mystring);
std::cout << "After: \n" << '#' << mystring << '#' << "\n\n";
return 0;
}
Output:

Before:
#Jane and Peter and Tom and Cindy#

After:
#Jane Peter Tom Cindy#

-Mike

Jul 19 '05 #5


lredmond wrote:

Can you give me a C++ example.


Don't top-post.

The solution I gave you WAS C++, it's just adapted from a C program I
wrote. If you are going to use char * types, that's all you need. If you
are going to use std::string, then there are other solutions. Read up on
them, take a stab, post your code.


Brian Rodenborn
Jul 19 '05 #6
Here is what I ended up doing using std::string

string line = "Tom and Peter and Jane and Joe and Bill";
string newline;

cout << "Before: " << line << endl;
std::string::size_type pos = 0;
std::string delim = " and ";
std::string newdelim = "\n";

while (( pos = line.find(delim,pos ) ) != std::string::npos )
{
line.replace(pos, delim.length(),newdelim);
pos +=newdelim.length();
}
cout << "After: " << line << endl;
--------------------------------------------------------------

"Default User" <fi********@company.com> wrote in message
news:3F***************@company.com...


lredmond wrote:

Can you give me a C++ example.


Don't top-post.

The solution I gave you WAS C++, it's just adapted from a C program I
wrote. If you are going to use char * types, that's all you need. If you
are going to use std::string, then there are other solutions. Read up on
them, take a stab, post your code.


Brian Rodenborn

Jul 19 '05 #7

"lredmond" <lr******@nyc.rr.com> wrote in message
news:Ph*****************@twister.nyc.rr.com...
Here is what I ended up doing using std::string

string line = "Tom and Peter and Jane and Joe and Bill";
string newline;

cout << "Before: " << line << endl;
std::string::size_type pos = 0;
std::string delim = " and ";
std::string newdelim = "\n";

while (( pos = line.find(delim,pos ) ) != std::string::npos )
{
line.replace(pos, delim.length(),newdelim);
pos +=newdelim.length();
}
cout << "After: " << line << endl;
--------------------------------------------------------------


Its not the most efficient since you repeatedly hack the same string. It
probably better to build up your output string as a completely seperate
string, copying over everything from the original string except the
delimiters.

john

Jul 19 '05 #8
"Jeremy Cowles" <je*************************@asifl.com> wrote in message news:<Wi*********************@twister.tampabay.rr. com>...
"Lans" <lr******@bn.com> wrote in message
news:19*************************@posting.google.co m...
I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain word

char *mystring "Jane and Peter and Tom and Cindy"
char *delim = " and ";
char *token;

token = strtok(mystring, delim);

while (token !=NULL )
{
//do some work
cout << token << endl;
token = strtok(NULL,delim)
}

my output would return JePererTomCiy
I need my output to be Jane Peter Tome Cindy
JePererTomCiy

That output doesn't make any sense, unless you mis-typed it... <?>


strtok writes a null character '\0' at the end of each token, and
takes tokens to be seperated by sequences of ' ', 'a', 'n' and 'd'
characters.
So, the first token is 'J', since the "an" after it is a delimiter
sequence.
The 'J' is null-terminated by strtok writing '\0' over the 'a', and
duly printed.
The next token returned is "Peter" (I assume the "Perer" was a typo).
The null terminator is written over the following space character, and
the token is printed.

.... and so on.
This clearly isn't what the author wanted, but the output looks
plausible for the code.

Jeremy

Jul 19 '05 #9

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:be**********@slb9.atl.mindspring.net...
| Lans <lr******@bn.com> wrote in message
| news:19*************************@posting.google.co m...

[snip]

| /* void replace_delims(std::string& s) */
| /* -- modifies argument 's' as follows: */
| /* */
| /* - Removes all occurrences of the string "and" which are */
| /* delimited by whitespace and/or end-of-string */
| /* */
| /* - Replaces consecutive whitespace characters with a */
| /* single space character */
| /* */
| /* - Removes leading and trailing whitespace */

Hey Mike.

Anyone would think you were a 'C' programmer :-).

Cheers.
Chris Val
Jul 19 '05 #10

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

Similar topics

16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
4
by: Kelvin | last post by:
hi: in C, we can use strtok() to tokenize a char* but i can't find any similar member function of string that can tokenize a string so how so i tokenize a string in C++? do it the C way? ...
2
by: James | last post by:
Hi, I am looking for a stringtokenizer class/method in C#, but can't find one. The similar classes in Java and C++ are StringTokenizer and CStringT::tokenize respectively. I need to keep a...
5
by: Lam | last post by:
Hi I try to read in a line from text file, and how can I tokenize the line? Thanks
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
1
by: Tim | last post by:
I ran into a problem with a script i was playing with to check code indents and need some direction. It seems to depend on if tabsize is set to 4 in editor and spaces and tabs indents are mixed on...
0
by: noobcprogrammer | last post by:
#include "IndexADT.h" int IndexInit(IndexADT* word) { word->head = NULL; word->wordCount = 0; return 1; } int IndexCreate(IndexADT* wordList,char* argv)
2
by: askalottaqs | last post by:
there's in maya's scripting language mel, called tokenize, you simply tokenize("string i want to tokenize"," ",bufferArray) which will fill the fufferArray wih the first string tokenized accorfing...
6
m6s
by: m6s | last post by:
1. After hours of researching, I used these snippets : void Object::TokenizeLines(const string& str, vector<string>& tokens, const string& delimiters) // Skip delimiters at beginning....
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.