473,663 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strtok - determine delimiter at certain position

Dear Guru,

I have been thinking hard on how to token based on demiliter after certain
position.
Example, I have list of possible string below, and the the delimiter is "1"
with the rules below
- if the 5th character is "1", then it is delimiter
elseif the 7th character is "1", then it is delimiter
else
this string is not a valid string
NE341LCBAA35
NE311LCBAA35
NE141LCBAA35
NE31341LCBAA35

I have code:

strcpy(szPrefix ,strtok(token," 1\n"));
strcpy(szSuffix ,strtok(NULL,"1 \n"));

but only able to detect the 1st occurence of "1", and treat it as delimiter,
based on the rules above, this is not valid if has string like NE311LCBAA35.

What I want to achieve is: If I have string NE311LCBAA35, my delimiter will
be the 5th "1", and my
szPrefix = "NE31"
szSuffix = "LCBAA35"

Could you share with me on how to achieve above rules ?

Many thanks.

P/S: this is not school homeworks or anything. I'm a working adult, just my
personal interest in programming.

Regards.


Sep 30 '06 #1
3 4049
"magix" <ma***@asia.com writes:
I have been thinking hard on how to token based on demiliter after certain
position.
Example, I have list of possible string below, and the the delimiter is "1"
with the rules below
- if the 5th character is "1", then it is delimiter
elseif the 7th character is "1", then it is delimiter
else
this string is not a valid string
For a simple case like this, just copying string segments around is
probably easier than trying to use strtok(). See my example program
below. (Error handling omitted for brevity)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
enum { MINDP = 4, MAXDP = 6 };
char *input, *prefix, *suffix;
size_t pos = 0;
size_t sl;

if (argc != 2) {
fprintf(stderr, "Usage: chop TEXT\n");
exit(1);
}

input = argv[1];
if (input[MINDP] == '1')
pos = MINDP;
else if (input[MAXDP] == '1')
pos = MAXDP;
else {
fprintf(stderr, "Error: invalid string\n");
exit(1);
}

sl = strlen(input) -pos -1;
prefix = malloc(sizeof(p os +1));
suffix = malloc(sl +1);
memcpy(prefix, input, pos);
memcpy(suffix, input +pos +1, sl);
prefix[pos] = suffix[sl] = '\0';

printf("Prefix: %s\nSuffix: %s\n", prefix, suffix);
return 0;
}

--
Ralph Moritz Ph: +27 846 269 070
GPG Public Key: http://ralphm.info/public.gpg

"Faith is believing something you know ain't true."
Sep 30 '06 #2
magix wrote:
Dear Guru,

P/S: this is not school homeworks or anything. I'm a working adult, just my
personal interest in programming.

Regards.
Yeah anyways, best to avoid strtok(), as it is a handicapped interface,
atleast in my opinion. For these types of routines, I typically just
use self-written stuff utilizing standard unix iovec structures
(although these are not necessarily required).

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#ifdef HAVE_UIO /* avoid comp.lang.c warnings */
#include <sys/uio.h>
#else
struct iovec {
void *iov_base;
size_t iov_len;
};
#endif

typedef struct iovec iov;

size_t iovtok(iov *v, size_t vc, const char *q, size_t sz, const char
*t, size_t tsz)
{
const char *p, *r;
size_t vct, tszt;

for (vct = 0, p = q, r = q + sz; vct < vc && q < r; q++) {
for (tszt = tsz; tszt; tszt--)
if (*q == t[tszt - 1]) break;

if (tszt == 0) {
continue;
} else if (q != p) {
v[vct].iov_base = (void *)p;
v[vct].iov_len = q - p;
vct++;
}

p = q + 1;
}

if (vct < vc && q != p) {
v[vct].iov_base = (void *)p;
v[vct].iov_len = q - p;
vct++;
}

return vct;
}

int main(int argc, char **argv)
{
char buf[1024];
size_t tl, c, vc, vct;
iov *v;

if (argc <= 2) return -1;
if ((vc = strtol(argv[1], 0, 10)) <= 0) return -1;
if ((v = malloc(sizeof(* v) * vc)) == NULL) return -1;

for (tl = strlen(argv[2]); fgets(buf, sizeof(buf), stdin); ) {
vct = iovtok(v, vc, buf, strlen(buf) - 1, argv[2], tl);

fprintf(stderr, "vct == %ld\n", (long)vct);
for (c = 0; c < vct; c++) {
fprintf(stderr,
"v[%ld].iov_len == %ld\n",
(long)c,
(long)v[c].iov_len);
fprintf(stderr,
"v[%ld].iov_base == \"%.*s\"\n",
(long)c,
(int)v[c].iov_len,
(char *)v[c].iov_base);
}
}

free(v);

return 0;
}
--

$ gcc -Wall -W -ansi -pedantic -g3 -o memsplt memsplt.c
$ ./memsplt.exe 10 "1" << EOF
NE341LCBAA35
NE311LCBAA35
NE141LCBAA35
NE31341LCBAA35
EOF
vct == 2
v[0].iov_len == 4
v[0].iov_base == "NE34"
v[1].iov_len == 7
v[1].iov_base == "LCBAA35"
vct == 2
v[0].iov_len == 3
v[0].iov_base == "NE3"
v[1].iov_len == 7
v[1].iov_base == "LCBAA35"
vct == 3
v[0].iov_len == 2
v[0].iov_base == "NE"
v[1].iov_len == 1
v[1].iov_base == "4"
v[2].iov_len == 7
v[2].iov_base == "LCBAA35"
vct == 3
v[0].iov_len == 3
v[0].iov_base == "NE3"
v[1].iov_len == 2
v[1].iov_base == "34"
v[2].iov_len == 7
v[2].iov_base == "LCBAA35"
I'm sure you can figure out the appropriate code logic to determine 5
vs 7, etc.

Sep 30 '06 #3
Groovy hepcat magix was jivin' on Sat, 30 Sep 2006 16:21:31 +0800 in
comp.lang.c.
strtok - determine delimiter at certain position's a cool scene! Dig
it!
>I have been thinking hard on how to token based on demiliter after certain
position.
Example, I have list of possible string below, and the the delimiter is "1"
with the rules below
- if the 5th character is "1", then it is delimiter
elseif the 7th character is "1", then it is delimiter
else
this string is not a valid string
That's incredibly simple.

if('1' == yer_string[4])
{
/* Delimiter is 5th character. */
}
else if('1' == yer_string[6])
{
/* Delimiter is 7th character. */
}
else
{
/* Invalid. */
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Oct 2 '06 #4

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

Similar topics

7
5703
by: Fernando Barsoba | last post by:
Hi, I'm using strtok() in the following way: void obtain_param(char *pmsg, CONF_PARAMS *cnf ) { char *s1, *s2; size_t msg_len; s1 = strtok (pmsg,":"); if (s1) {
6
1097
by: plmanikandan | last post by:
Hi, I need to split the value stored in a string and store them to another charrecter array.I am using strtok function.But i am getting invalid output when there is no value between delimiter my code #include<stdio.h> #include<string.h> #include<stdlib.h> void main()
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 (checks omitted) char tst = "this\nis\n\nan\nempty\n\n\nline"; ^^^^ ^^^^^^ char *tok = strtok(tst, "\n");
4
2725
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
26
4406
by: ryampolsky | last post by:
I'm using strtok to break apart a colon-delimited string. It basically works, but it looks like strtok skips over empty sections. In other words, if the string has 2 colons in a row, it doesn't treat that as a null token, it just treats the 2 colons as a single delimiter. Is that the intended behavior?
5
25797
by: Kelly B | last post by:
I need a function which returns me a "word" from a given string and then sets the pointer to the next one which is then retrieved during further calls to the function. I think strtok( ) is the solution but i could not understand the use of the function as given in the C99 standard EXAMPLE #include <string.h> static char str = "?a???b,,,#c";
8
4496
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", "passwort" from "users" order by "users"
8
309
by: Stu Cazzo | last post by:
Hi all, I have a question on why strtok is doing what it's doing for my splitString( string2 ); call. Below is the output for the entire program: token was: word1 token was: word2 token was: word3 token was: word1
11
17169
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5657
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.