You are viewing a single comment's thread. Return to all comments →
Yeah, you can definitely use gets(). I used scanf() because it made this particular problem a lot easier.
Though, it depends a lot on the requirement, in general, I would suggest you to never use gets(). It's use is highly discouraged due to no protection to buffer overflow.
You get strong formatting features with scanf(), but its difficult (although possible) to use it properly with string buffers and impossible to be used safely for arithmetic input. Also, there can be failed scan or trailing newline in case input isn't well formatted. Hence, use scanf() only if you are completely aware of its functionality and know what you are doing (not recommended for new coders).
Use fgets(). It lets you specify buffer size. Just remember to remove trailing newLine (this also ensures buffer overflow protection).
In short, use fgets(), avoid scanf() and never use gets().
For this particular problem, you can use fgets() and pass it to sscanf() for scanf() like behavior, in case, you want to avoid scanf().