convert string to integer

Discussion in 'C Programming' started by shanmugaster, Sep 18, 2006.

  1. shanmugaster

    shanmugaster Guest

    Hi,
    I have a string array which may contain 5 digit values or NULL or
    just blank spaces. I should print the value of array as an integer.
    that is it should print 0 when it encounters NULL and blank spaces.
    when i used atoi, values greater than 32767 are converted into negative
    values. can some one please help me in this regard.

    sample values
    55008
    55009
    NULL

    55110
    NULL

    Thanking u in advance

    Star
     
    shanmugaster, Sep 18, 2006
    #1
    1. Advertisements

  2. That implies that on your system, int is not large enough to hold
    some of the values. So don't use int: use long instead. There is
    an equivilent to atoi() that is for long instead of it: you should
    be able to find it easily.
     
    Walter Roberson, Sep 18, 2006
    #2
    1. Advertisements

  3. Walter Roberson said:
    Poor advice, since it will lead him to atol() rather than strtol().
     
    Richard Heathfield, Sep 18, 2006
    #3
  4. shanmugaster

    rama_archana Guest

    Hi,

    how to reverse a string recursively in place

    thank you
     
    rama_archana, Sep 18, 2006
    #4
  5. shanmugaster

    rama_archana Guest

    Hi,

    how to reverse a string recursively in place

    thank you
     
    rama_archana, Sep 18, 2006
    #5
  6. How about sticking to the topic of this thread ? If
    you have another topic in mind you are free to start
    a new thread.
     
    Spiros Bousbouras, Sep 18, 2006
    #6
  7. The problem was *defined* in terms of restricted sets of input
    for which atol() and strtol() will be equivilent.

    strtol() is certainly a better choice in the general case, when
    input might vary more than was defined for this situation.
     
    Walter Roberson, Sep 18, 2006
    #7
  8. Why do you want to do it recursively? An iterative solution is likely
    to be more straightforward and more efficient.

    The only reason I can think of for that requirement is that this is a
    homework assignment. Is it?
     
    Keith Thompson, Sep 18, 2006
    #8
  9. shanmugaster

    CBFalconer Guest

    What a silly idea. I see no purpose to using recursion here.
     
    CBFalconer, Sep 18, 2006
    #9
  10. this is likely to be homework, so we won't do it all for you. Here's a
    few hints:

    Recursion can take you down, down, down the string to the end. At that
    point, if you start returning, you'll be encountering the characters in
    reverse order. What could you do at that point that would give you a
    reversed string?

    "C" is a fine language for this kind of exercise. You should be able
    to do this in like 5 to 8 lines or so.
     
    Ancient_Hacker, Sep 18, 2006
    #10
  11. Walter Roberson said:
    You do have a point but, as the the OP pointed out, the input data may
    contain NULL (by which I guess he means the empty string) or "just blank
    spaces". The strtol function will enable these cases to be detected,
    whereas atol will IIRC simply return 0.

    Now, it's certainly true that he said he just wanted these to be interpreted
    as 0, which I missed, and which is why you have a point. But when he
    discovers how simple it is to detect them, he may prefer to take advantage
    of the extra functionality that strtol() offers.

    Shall we call this one 70-30 in your favour? :)
     
    Richard Heathfield, Sep 18, 2006
    #11
  12. shanmugaster

    Default User Guest

    Don't hijack threads. You posted this in reply to "convert string to
    integer". Go figure you how to use Google to start a new thread.




    Brian
     
    Default User, Sep 18, 2006
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.