22
Dec
String to Integer (atoi)
Related Blog Items
- Convert integer to binary string
- i64toa - Converts a 64 bit integer to string
- C/C++ Interview algorithms
- Unsigned long to string (ultostr)
- const correctness
The question was to convert a VALID string into its corresponding integer value.
Solution:
int atoi(char *string)
{
int value=0,sign=1;
if ( !string || !*string ) return 0;
if ( *string == ‘-’ ) {
sign = -1;
string++;
}
while ( *string ) {
if ( (*string >= ‘0′) && (*string <= ‘9′) ) {
value = value * 10 + (*string - ‘0′);
}
else
break;
string++;
}
return sign * value;
}
[tags]atoi,string to integer,atoi source code,atoi code,atoi program,string to integer program[/tags]
Popularity: 10%
You need to log on to convert this article into PDF
Related Blog Items - Convert integer to binary string
- i64toa - Converts a 64 bit integer to string
- C/C++ Interview algorithms
- Unsigned long to string (ultostr)
- const correctness
Related Blog Items
- Convert integer to binary string
- i64toa - Converts a 64 bit integer to string
- C/C++ Interview algorithms
- Unsigned long to string (ultostr)
- const correctness
No Comments
No comments yet.