[Solved] C string tokenizer with space delimiter does not work as expected on SL code playground with user provided input. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

[Solved] C string tokenizer with space delimiter does not work as expected on SL code playground with user provided input.

https://code.sololearn.com/cX0AG1QsY6ID/?ref=app

13th Oct 2020, 11:11 AM
Sonic
Sonic - avatar
10 Answers
+ 8
scanf() is designed to stop reading in characters when it encounters a whitespace character. If you use fgets() instead of scanf() in the first example, it works just as fine.
13th Oct 2020, 11:19 AM
Shadow
Shadow - avatar
+ 7
Martin Taylor thanks. I didn't know about %*c
14th Oct 2020, 2:42 AM
Sonic
Sonic - avatar
+ 6
Théophile thanks. Shadow indicated that too.
13th Oct 2020, 11:21 AM
Sonic
Sonic - avatar
+ 6
Coder Kitten I didn't know that option. Thanks!
13th Oct 2020, 2:04 PM
Sonic
Sonic - avatar
+ 5
I was trying to test the code in the above question with space delimited words as input from the SL code playground input but the tokenizing didn't work as I expected. Any ideas on what I may be doing wrong?
13th Oct 2020, 11:13 AM
Sonic
Sonic - avatar
+ 5
Shadow brilliant! Thanks.
13th Oct 2020, 11:20 AM
Sonic
Sonic - avatar
+ 4
On the other hand, if I provide the string to be tokenised within the code itself, it works. https://code.sololearn.com/cZLyNIM4ETvY/?ref=app
13th Oct 2020, 11:14 AM
Sonic
Sonic - avatar
+ 4
It might be because of scanf. Try to get a whole line instead. (try with gets, maybe?)
13th Oct 2020, 11:20 AM
Théophile
Théophile - avatar
+ 3
Sonic indeed, I didn't see the Shadow answer... 😅 Sorry! (I think I was writing at the moment Shadow answered).
13th Oct 2020, 11:29 AM
Théophile
Théophile - avatar