How can I check if a string is a positive integer in C, with nothing else attached with it?
I have tried using atoi(), but atoi would still give me a legit value(not 0)
even if the string is "27abc"
How can I check if a string is a positive integer in C, with nothing else attached with it?
I have tried using atoi(), but atoi would still give me a legit value(not 0)
even if the string is "27abc"
Depends on how lenient, if at all, you want to be. strtol() is a better version of atoi(), and can tell you if it ran into a character that it couldn't convert (the 'a' in "27abc", for example). But strtol() allows leading whitespace, which might not suit you.
You could always loop through the string yourself and verify that each character is a digit with isdigit().