473,498 Members | 37 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strtok but keep the separator

I need a way to use strtok or some other function to parse a string but keep
the original separator.

My string that is delimited by commas and spaces and I need to format the
string for output so that there is no breaks on the tokens.

So (notice no spaces after the commas):
"Mary had a little lamb,sheep,goat,frog and a couple of other herding
animals"

would be displayed on a 20 character output area as:
12345678901234567890
"Mary had a little lamb,
sheep,goat,frog and a
couple of other herding
animals"

Thanks,
Steve
Nov 14 '05 #1
3 3907
"Steven" <no****@for.me> wrote in message
news:41***********************@news.twtelecom.net. ..
I need a way to use strtok or some other function to parse a string but keep
the original separator.

My string that is delimited by commas and spaces and I need to format the
string for output so that there is no breaks on the tokens.

So (notice no spaces after the commas):
"Mary had a little lamb,sheep,goat,frog and a couple of other herding
animals"

would be displayed on a 20 character output area as:
12345678901234567890
"Mary had a little lamb,
sheep,goat,frog and a
couple of other herding
animals"


strtok() is not what you need for this, simple pointer pushing will do.

NEVER use strtok(), it is rubish left over for historical compatibility.
If you need its functionality, write your own function with proper semantics.

--
Chqrlie.
Nov 14 '05 #2


Charlie Gordon wrote:
"Steven" <no****@for.me> wrote in message
news:41***********************@news.twtelecom.net. ..
I need a way to use strtok or some other function to parse a string but keep
the original separator.

My string that is delimited by commas and spaces and I need to format the
string for output so that there is no breaks on the tokens.

So (notice no spaces after the commas):
"Mary had a little lamb,sheep,goat,frog and a couple of other herding
animals"

would be displayed on a 20 character output area as:
12345678901234567890
"Mary had a little lamb,
sheep,goat,frog and a
couple of other herding
animals"

Note: It is at best strange that you want _at_least_ 20 characters
instead of _at_most_. You may want to consider specifying ranges
like, say, 20-30 characters to avoid having lines breaking where
they should not.

Something along these lines may get you started:
__________________________________________________ _____________

#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* Not needed for PrintStringLinewise() */

#define SEPARATORS " ,"
#define WHITESPACES " \n\t\r"

void PrintStringLinewise (const char *string, int linewidth,
int direction)
{
long int maxoffset;
int offset;
const char *searchpos;

direction = (direction < 0) ? -1 : 1;
maxoffset = (long int) strlen(string);
offset = 0;
searchpos = string;

while (offset < maxoffset) {
if ((offset = linewidth) > maxoffset) {
puts(searchpos);
break;
}
while (offset > 0 && searchpos[offset]) {
if (strchr(SEPARATORS, searchpos[offset]))
{
if (direction < 0) {
while (strchr(SEPARATORS, searchpos[offset]))
offset--;
offset++;
}
if (strchr(WHITESPACES, searchpos[offset]))
offset--;
break;
}
offset += direction;
}
if (searchpos[offset])
offset++;
if (offset == 1)
offset = linewidth;
printf("%.*s\n", offset, searchpos);
searchpos += offset;
maxoffset -= offset;
offset = 0;
/* Gobble blanks */
while (*searchpos && strchr(WHITESPACES, *searchpos)) {
maxoffset--;
searchpos++;
}
}
}

int main (int argc, char **argv)
{
int i, linewidth = 23;
const char *teststring =
"Mary had a little lamb,sheep,goat,frog and a couple"
" of other herding animals";
const char *teststring2 =
"veryveryveryveryveryveryveryveryveryveryveryveryv ery"
"veryverylongstring";

if (argc>1)
linewidth = atoi(argv[1]);

for (i=1; i<=linewidth; i++)
putchar((i%10)+'0');
putchar('\n');
PrintStringLinewise(teststring, linewidth, -1);
for (i=1; i<=linewidth; i++)
putchar((i%10)+'0');
putchar('\n');
PrintStringLinewise(teststring, linewidth, +1);
for (i=1; i<=linewidth; i++)
putchar((i%10)+'0');
putchar('\n');
putchar('\n');
for (i=1; i<=linewidth; i++)
putchar((i%10)+'0');
putchar('\n');
PrintStringLinewise(teststring2, linewidth, -1);
for (i=1; i<=linewidth; i++)
putchar((i%10)+'0');
putchar('\n');
PrintStringLinewise(teststring2, linewidth, +1);
for (i=1; i<=linewidth; i++)
putchar((i%10)+'0');
putchar('\n');
return 0;
}
__________________________________________________ _____________

Note: I did not perform any tests apart from the ones visible
in main(), so check whether everything is as it should be.
You also might want to consider whether int/long int are
the appropriate types for linewidth, offset and maxoffset.
Comments are left as exercise to the reader.

strtok() is not what you need for this, simple pointer pushing will do.
Agreed.

NEVER use strtok(), it is rubish left over for historical compatibility.
If you need its functionality, write your own function with proper semantics.


If I know about the limitations of strtok() and if strtok() is
what I need, then I will use it.
However, as most newbies will get a piece of sample code and
"adapt" it for their purposes, yours is probably good advice
for them.

One "good" use of strtok() certainly is obfuscating... ;-)
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #3
Steven wrote:
I need a way to use strtok or some other function to parse a string but keep
the original separator.


Copy the string; keep the original; work on the copy.
Nov 14 '05 #4

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

Similar topics

5
3849
by: krini_pop | last post by:
I have a string that is delimited by commas. I'm using strtok and putting the values in a vector. In some cases, I may have 2 commas side by side and therefore need it to insert a null value. Right...
13
4891
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
33
1022
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in...
8
1912
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
18
3459
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured...
8
2127
by: cting76 | last post by:
If I understand it correctly, strtok scans from the first character after the separator. The code below: #include <iostream> #include <fstream> using namespace std; void main() { char...
8
4471
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
6
4365
by: Sheldon | last post by:
Hi Everyone, I have a file with a set of strings per line and each line ends with a '\n' and each string separated by a whitespace. After reading each line and using strtok() to split the string...
12
2335
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
7124
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
7163
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
7200
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...
1
6884
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
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.