how to convert"-123" pointer string to int -123 without using atoi

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thyagi
    New Member
    • Nov 2007
    • 2

    how to convert"-123" pointer string to int -123 without using atoi

    a function which takes a string (pointer to char) as a parameter and returns the
    integer equivalent.str2 int("1658") would return the value 1658
    To improve your program change the function to accept strings with a leading minus sign toi ndicate a negative number.
    One last improvement might be to print a warning if the string represents a numberthat is outside the range of an int.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by thyagi
    a function which takes a string (pointer to char) as a parameter and returns the
    integer equivalent.str2 int("1658") would return the value 1658
    To improve your program change the function to accept strings with a leading minus sign toi ndicate a negative number.
    One last improvement might be to print a warning if the string represents a numberthat is outside the range of an int.

    I think you have to write your own atoi function.
    You may get atoi code easily by searching in the net.
    Post again if your requirement is different.
    Raghuram

    Comment

    • Cucumber
      New Member
      • Sep 2007
      • 90

      #3
      I never liked atoi, is it standard?

      I always used sscanf().

      Comment

      • manjuks
        New Member
        • Dec 2007
        • 72

        #4
        Code removed per posting guidelines
        Hope this will help you.

        Thanks,
        Manjunath

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Why all this complex code???

          The requirment was that atoi() couldn't be used. So why not just call strol() and call it a day???

          Comment

          Working...