is there any readily available split function in C, to split a string up when a delimiter is found? my delimiter is not just one character, but 2 characters, is it possible in C?
Printable View
is there any readily available split function in C, to split a string up when a delimiter is found? my delimiter is not just one character, but 2 characters, is it possible in C?
yeah
but if i'm not mistakened, strstr onli returns the location, but how do i actually split it up?
>but how do i actually split it up?
strcpy/strncpy?
Can you provide an example of what you are trying to do? For example, I want to take "Hello--world" and put "Hello" in one string and "world" in another.
There are other string functions in the standard library that may be better suited to the task. But a better description will result in better replies.
I think the answer is in the Programming FAQ.
Do a search for strtok. That should help.
Is this what you're looking for?
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *sbreak(char *s, char *tok, char *del)
{
char *new_s;
char *p = tok;
/* If no more string, bail */
if (*s == '\0')
return 0;
/* If no delimiters, seek to end */
if ((new_s = strstr(s, del)) == 0)
new_s = s + strlen(s);
while (s != new_s)
*p++ = *s++;
*p = '\0';
/* Bump past the delimiter if it's there */
if (*new_s)
s += strlen(del);
return s;
}
int main(void)
{
char a[10];
char s[] = "this::is::a::::test";
char *p = s;
while ((p = sbreak(p, a, "::")))
{
if (*a)
printf("%s\n", a);
}
return 0;
}
thanx ppl, will look thru wat u said...
oh, wat i want to do is actually once i have a string, like
hello||test
i want to detect the delimiter which is "||" and then split and assign hello and test to different variables...