Issues splitting a string into 2 halves in C -


I have a string with an empty space, such as "print 2" or "print 3 exam". I'm trying to delete the argument first - in these examples, print.

I have tried strtok ():

  char * test; Test = strtok (COMMAND, ""); Printf ("% s \ n", test);   

Although the printing test will be the default. I tried to make a function, and it works fine with the main (), but when the function is said in which I need it, then it also segfaults.

  char * split (char s [], int n) {printf (the partition that runs on "% s \"), s); Int spaces = 1; Int i = 0; For (i; i & lt; strlen (s) and space & lt; = n; i ++) {if (s [i] == '') {space ++; }} Printf ("Partition:% s \ n", & S [i]); // Return "0"; Return and S [i]; }   

I think this is some kind of pointer problem. The command is being passed to the print function such as:

  print (file, lines, command);  

I do not know what the command in your test sample is, but you should check, If the strtok returns the tap (when a strotok can not find a token) printf with a nullpointer will give you a seg volt.

Generally you call strtok with a loop:

Comments