0

I have a string variable (It is a input from the user space ) and i need to pull two number inside of it . For example :

"123 + 12" i need to create 2 substring from it . "123" and "12" .

Format of this string always will be like in the examples : "12 + 1" , "1 * 1" , "432 - 232" etc so length of the string isn't a constant value . It is the biggest problem for me .

How can i take these two numbers from the string and convert them to two integer value ? Can you show me a simple example ? How can i split the string by space character in Linux Kernel ?

anor
  • 43
  • 1
  • 7
  • 1
    Possible duplicate of [Can I use strtok() in a Linux Kernel Module?](http://stackoverflow.com/questions/2246618/can-i-use-strtok-in-a-linux-kernel-module) – Tim Dec 07 '16 at 01:03
  • strtok() and strsep() looks different . char *strtok(char *str, const char *delim) , char *strsep(char **s, const char *ct) @Tim – anor Dec 07 '16 at 01:23
  • I was doing something just like this the other day. I used `strspn` and `strcspn` to skip whitespace and isolate the numbers. I used`kstrtoint` to convert the numbers to integers. You'll have the additional task of checking for that `+` sign. – Steve Summit Dec 07 '16 at 04:17
  • @anor, you have to be careful when parsing something like `3 - 2` vs. `3 + -2` vs. `3 -2` in case you are using signed values. Otherwise the idea is pretty simple, either you are using `sscanf()` or `isdigit()` + `kstrto*()` in a loop. – 0andriy Dec 11 '16 at 15:25

0 Answers0