Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Nov 2007
Posts: 2
#1: Dec 4 '07
a function which takes a string (pointer to char) as a parameter and returns the
integer equivalent.str2int("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's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Dec 4 '07

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


Quote:

Originally Posted by thyagi

a function which takes a string (pointer to char) as a parameter and returns the
integer equivalent.str2int("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
Member
 
Join Date: Sep 2007
Posts: 90
#3: Dec 4 '07

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


I never liked atoi, is it standard?

I always used sscanf().
Member
 
Join Date: Dec 2007
Location: Bangalore India
Posts: 54
#4: Dec 4 '07

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


Code removed per posting guidelines
Hope this will help you.

Thanks,
Manjunath
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#5: Dec 4 '07

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


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