Thread: Check if a string is a positive integer, please help.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Check if a string is a positive integer, please help.

    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"

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,010
    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().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 02:18 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
Website Security Test