0

I want to split a string i have "msg 10 2" into different strings & ints. so instead of having

  msg 10 2

I can take each as a seperate parameter can print:

   msg
   10
   2      

I use the variable to define a message:

  char msg[30] = "msg 10 2";

I then want to take each field as seperate values/parameters.

Thanks

  • Since you have `strtok` in your tag, so you already know what to do. What is the problem you are having? – SSC Oct 30 '14 at 02:31

2 Answers2

1

If you know your string will always follow the format string int int, then you could also use sscanf.

ipavl
  • 828
  • 11
  • 20
0

Use strtok (which you already tagged) and atoi.

hushaohan
  • 83
  • 5
  • i dont know how to use it for this case. –  Oct 30 '14 at 02:48
  • @user3035890: The [`strtok` reference page](http://www.cplusplus.com/reference/cstring/strtok/) has example code of tokenizing a string. [`atoi`](http://www.cplusplus.com/reference/cstdlib/atoi/) simply converts a cstring (`"10"`) to an int (`10`). – hushaohan Oct 30 '14 at 05:36