How to check whether a string is number

Discussion in 'C' started by qiqinuinaifen128, Jan 15, 2008.

  1. qiqinuinaifen128

    qiqinuinaifen128 New Member

    Joined:
    Dec 10, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Co-ordinator of swimming lessons
    Location:
    Singapore
    Home Page:
    http://www.swimmingclasses.com.sg
    if i have a string, how do i check whether the string is number?
     
  2. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    look for atoi() function...
    it will return zero if it is not able to convert the string passed into number..

    though if you have an alphanumeric inputs (like 1234go4expert)..u should go for strtol()..

    both the functions even convert the input passed into their respective numbers(if possible).
     
  3. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    You should use strtol() all the time.
    atoi() for example cannot detect numeric overflow.
     
  4. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    completely agree..
    there is another kind of case ..when input is "1234forum"
    atoi() will return 1234..!!

    and in case of strtol() using the third argument we can check if the function traversed the whole input or not..
     
  5. qiqinuinaifen128

    qiqinuinaifen128 New Member

    Joined:
    Dec 10, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Co-ordinator of swimming lessons
    Location:
    Singapore
    Home Page:
    http://www.swimmingclasses.com.sg
    Thank you so much. I will try it
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice