Originally Posted by
oror84
hello,
i have a problem to split a string.
i have this sentence :
char str1[100] = "hello this is my sentence and max(a,b) is my method";
i want to to split the sentence from the max(a,b) to 2 strings.
with :
char *str2;
str2 = strstr(str1,"max");
now i took the right side of the sentence to str2.
bow how can i take the left side to other string?
maby how can i get the place of max by numbers(lenght)?
i didnt find any str command to do this.
i`ll happy if someone help me.
Thanks ,
Or.
Ok... you need to do three things here...
Code:
#include <stdlib.h>
#include <string.h>
int main (void)
{ // testing example
char test[] = "hello this is my sentence and max(a,b) is my method";
char *Left;
char *Right;
// first make a copy
Left = strdup(test);
// second locate the desired text
Right = strstr(Left,"max");
// third split the string
*(Right - 1) = '\0';
// print the results
printf("Original : %s\nLeft side: %s\nRight side: %s\n\n",test,Left,Right);
// clean up the mess
free(Left);
// and exit
return 0; }