convert string to integer

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

  1. 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
    , Sep 18, 2006
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    > I have a string array which may contain 5 digit values


    >when i used atoi, values greater than 32767 are converted into negative
    >values. can some one please help me in this regard.


    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.
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
    Walter Roberson, Sep 18, 2006
    #2
    1. Advertising

  3. Walter Roberson said:

    > In article <>,
    > <> wrote:
    >> I have a string array which may contain 5 digit values

    >
    >>when i used atoi, values greater than 32767 are converted into negative
    >>values. can some one please help me in this regard.

    >
    > 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.


    Poor advice, since it will lead him to atol() rather than strtol().

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 18, 2006
    #3
  4. Guest

    how to reverse a string recursively

    Hi,

    how to reverse a string recursively in place

    thank you
    , Sep 18, 2006
    #4
  5. Guest

    how to reverse a string recursively

    Hi,

    how to reverse a string recursively in place

    thank you
    , Sep 18, 2006
    #5
  6. wrote:

    > Hi,
    >
    > how to reverse a string recursively in place
    >
    > thank you


    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. In article <>,
    Richard Heathfield <> wrote:
    >Walter Roberson said:


    >> an equivilent to atoi() that is for long instead of it:


    >Poor advice, since it will lead him to atol() rather than strtol().


    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.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
    Walter Roberson, Sep 18, 2006
    #7
  8. Re: how to reverse a string recursively

    writes:
    > how to reverse a string recursively in place


    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 (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 18, 2006
    #8
  9. CBFalconer Guest

    Re: how to reverse a string recursively

    wrote:
    >
    > how to reverse a string recursively in place


    What a silly idea. I see no purpose to using recursion here.

    --
    "The most amazing achievement of the computer software industry
    is its continuing cancellation of the steady and staggering
    gains made by the computer hardware industry..." - Petroski



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 18, 2006
    #9
  10. Re: how to reverse a string recursively

    wrote:
    > Hi,
    >
    > how to reverse a string recursively in place
    >
    > thank you


    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:

    > In article <>,
    > Richard Heathfield <> wrote:
    >>Walter Roberson said:

    >
    >>> an equivilent to atoi() that is for long instead of it:

    >
    >>Poor advice, since it will lead him to atol() rather than strtol().

    >
    > The problem was *defined* in terms of restricted sets of input
    > for which atol() and strtol() will be equivilent.


    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
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 18, 2006
    #11
  12. Default User Guest

    Re: how to reverse a string recursively

    wrote:

    >
    > Hi,


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Paskett

    Convert a Base32 string to Base10 integer

    Jon Paskett, Dec 18, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,112
    Nathan Sokalski
    Dec 18, 2005
  2. Azmie
    Replies:
    12
    Views:
    1,066,696
    Azmie
    Dec 30, 2003
  3. les ander
    Replies:
    4
    Views:
    873
    wes weston
    Oct 5, 2004
  4. news
    Replies:
    2
    Views:
    1,109
  5. Randy Kramer
    Replies:
    12
    Views:
    369
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page