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
|
|
0
|
|
|
|
Reply
|
nospam239 (160)
|
12/1/2004 2:00:45 PM |
|
"Steven" <nospam@for.me> wrote in message
news:41adce8d$0$74192$39cecf19@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.
|
|
0
|
|
|
|
Reply
|
news186 (632)
|
12/1/2004 2:54:02 PM
|
|
Charlie Gordon wrote:
> "Steven" <nospam@for.me> wrote in message
> news:41adce8d$0$74192$39cecf19@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 =
"veryveryveryveryveryveryveryveryveryveryveryveryvery"
"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.
|
|
0
|
|
|
|
Reply
|
Michael.Mair (1492)
|
12/1/2004 5:56:53 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
12/1/2004 6:43:59 PM
|
|
|
3 Replies
32 Views
(page loaded in 0.93 seconds)
Similiar Articles: Split string at specific character - comp.soft-sys.matlab ...... is not confused by back/forward slashs, but allows any string to be used as separator. ... > > Thanks, Jan See STRTOK s = '123;456;567' c= {} ; while ~isempty(s ... parsing alpha and numeric characters out of string - comp.lang.awk ...i keep seeing things like /[A-Za-z]+/ in the book, but so far haven't been able ... my awk/sed o'reilly book i realize that the -F option > just changes what the separator ... Data with "," and field sepeator is ",", How to handle this - comp ...Something to keep in mind when using $0 as “the original input line ... (Most likely $0.) > # sep, the separator between the values. > # > # After a call to ... The Separation Model - comp.databases.filemakerI just read an article in Filemaker Advisor (Jan '04) about "The Separation Model" (TSM ... fields, creating layouts, > writing scripts, building reports), you keep your data ... Use of quotation marks and apostrophes in SQL and Criteria ...I keep having problems understanding the use of apostrophes and quotation marks ... them into an And either gives me a type mismatch or a > List separator ... separating speech and music in a sound file - comp.soft-sys.matlab ...But keep in mind, if the music has significant energy in that frequency region ... Blind Source Separation: Audio Examples - CNL : The Computational ... Blind Source ... makeing changes to a file with awk - comp.lang.awkchange field separator - comp.lang.awk Hi, I was wondering if there were an easy way to ... files with find/awk... the output of find to awk, but the > problem I keep running ... programmatically print PDF and specify printer, page size, and ...... to specify printer settings like copy count, page size and orientation, and separator ... comp.databases.filemaker ..... printer you set the Page ... copy but keep the ... how to merge multiple lines into one line - comp.lang.awk ...Further, if I change how I do ng correspondence, I need to keep my email format in ... this instead: awk -v RS= '$1=$1' filename The above says that the record separator ... insert a decimal point - comp.soft-sys.sasformat number with thousand separator - comp.lang.awk I know how to put the dec ... How may decimal places SAS keep when do calculation - comp.soft ... insert a ... strtokThe strtok() function searches for a separator string within a larger string. ... This function uses static storage to keep track of the current ... Boost Char Separator - Boost 1.39.0 - Boost C++ LibrariesThe behaviour of strtok() is to skip ahead. The char_separator class provides both ... tokenizer; boost::char_separator<char> sep("-;", "|", boost::keep_empty ... 7/9/2012 5:11:28 PM
|