I have a string that occurs in this format:
.word 40
I would like to extract the integer part. The integer part is always different but the string always starts with .word
. I have a tokenizer function which works on everything except for this. When I put .word
(.word with a space) as a delimiter it returns null.
How can I extract the number?
Thanks
Quick and dirty:
The asterisk tells scanf not to store the string it reads. Use fscanf if you're reading from a file, or sscanf if the input is already in a buffer.
Check the string with
If this returns 0, then your string starts with ".word " and you can then look at (your string) + 6 to get to the start of the number.
This code write in console all digits.
You can use strtok() to extract the two strings with space as an delimiter.
Online Demo:
Output:
If you want the number
40
as a numeric value rather than a string then you can further use atoi() to convert it to a numeric value.You can use sscanf to extract formated data from a string. (It works just like scanf, but reading the data from a string instead of from standard input)