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 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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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",...
|
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...
|
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)...
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |