convert string to integer

S

shanmugaster

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
 
W

Walter Roberson

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

Richard Heathfield

Walter Roberson said:
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().
 
S

Spiros Bousbouras

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

Walter Roberson

Walter Roberson said:
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.
 
K

Keith Thompson

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?
 
A

Ancient_Hacker

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

Richard Heathfield

Walter Roberson said:
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? :)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top