question about "strtok"

hi everyone.
i learned how to use strtok from here: "http://www.cplusplus.com/reference/cstring/strtok/"

in the example he means that the "brake" char can be "," or "." or....
i want that my "brake" will be the string ",.", means that if i get
the string "abc,def" - it will not split,
but if the string is "abc,.def" - it will split to "abc" "def"

how can i do so??

thanks..
The function you are looking for is strstr

http://www.cplusplus.com/reference/cstring/strstr/
can u explain please how to use it? i tried to do the example below, according to my program and what i get it :

scanf(" %s",&longString); //longString=abc.,def
splitting = strstr (longString, myBrake); //myBrake=".,"
//after that line i get that splitting =.,def instead of abc

what i did wrong?
there's a decent example in sloth's link.
you didn't understand me. i don't want to delete the part "abc".
i want to keep him somewhere.
so if i have x="abc.,def" and i will do something, that will lead to:
y="abc"
z="def"

the question is what is that "something". maybe i'm stupid but i don't see it on the example below. u just delete.
Here's an example, expanding the example from @SlothFan's link. You'll have to extend it to make it more general and safe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* strstr example */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main ()
{
  const char *str = "abc.,def";
  const char *delim = ".,";
  char * pch;
  pch = strstr (str,delim);
  size_t len = pch - str;
  char *x = (char *) calloc(len + 1, sizeof(char));
  strncpy (x, str, len);
  len = strlen(str) - len - strlen(delim);
  char *y = (char *) calloc(len + 1, sizeof(char));
  strncpy(y, pch + strlen(delim), len);
  
  printf("str = %s\n", str);
  printf("x = %s\n", x);
  printf("y = %s\n", y);
  
  free(y);
  free(x);
  
  return 0;
}
thanks a lot. but i have one last question. if i want to do it with loop,
so if x="abc.,def.,ghi"=====>>>>>>y=abc
z=def
z2=ghi

in strtok i deleted what i already, used by:
pch = strtok (NULL, delim);

how can i do it here?

i can't write NULL. it gives me an error message.
and i'm not sure for what i need "len".

thanks a lot again
somebody?
I don't quite get what you mean by "deleted what I already used", here's an example illustrating what I mean by extending it. This should do arbitrary length null-terminated strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/* remember to do error checking, especially with the memory allocation and string length operations */
int main ()
{
  const char *input_str = "abc.,def.,ghij.,klmn.,opq.,rst";
  const char *str = input_str;
  const char *delim = ".,";
  const size_t str_length = strlen(str);
  const size_t delim_len = strlen(delim);
  char *pch = 0;
  printf("str = %s\n", str);
  while (pch = strstr(str, delim))
  {
	  size_t len = pch - str;
	  pch += delim_len;
	  char *x = (char *) realloc(0, len + 1);
	  memset(x, 0, len + 1);
	  strncpy(x, str, len);
	  str = pch;
	  printf("x = %s\n", x);
	  free(x);
  }

  printf ("last token = %s\n", str);
  return 0;
}
Topic archived. No new replies allowed.