0

I got first 5 digits that got the course serial number and then the course name i want to split serial number from name to save in a struct for example:

19234 Programming 101

I want to split to:

array[0]=19234
array[1]=programming 101

Thanks.

  • 4
    Doesn't `strtok()` do what you want? – Barmar Dec 18 '20 at 16:15
  • @Barmar Do not use `strtok()`, `strtok()` uses `static` variables which makes it behave unpredictable in multithread environments and unexpected in cases where a subroutine and a higher routine both use `strtok()`. `strtok_r()` is better. – 12431234123412341234123 Dec 21 '20 at 12:30

1 Answers1

0

Very simple you can use the function strtok() as it mensioned before from barmar. strtok() is a functiont that takes two parametres first you have to insert your string that you want to split and in the second parametre you declare inside in " here " what do you want to split .You can simple use " " for vacuum or commas and etc. example :

int main(){
      char s[SIZE]="19234 Programming 101";
      char *c; // needs to be pointer

      c=strtok(s," ");
      printf("%s",c); // it will print this '19234'

      return 0;
}
gregni
  • 417
  • 3
  • 12
  • Do not use `strtok()`. I made an answer why you should not use `strtok()` here: https://stackoverflow.com/questions/15472299/split-string-into-tokens-and-save-them-in-an-array/65107407#65107407 – 12431234123412341234123 Dec 21 '20 at 12:27
  • @12431234123412341234123 i will not follow that , sorry – gregni Dec 21 '20 at 15:16
  • If you are on Linux, Windows, Mac, Anroid, ... you could just replace `strtok()` with `strtok_r()` or `strtok_s()` and pass a pointer by pointer which you do not use for anything else. `char *context; strtok_r(s," ",&context);`. – 12431234123412341234123 Dec 21 '20 at 15:26