Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

22
Dec

String to Integer (atoi)

Related Blog Items

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: 14%

You need to log on to convert this article into PDF


Related Blog Items

No Comments

No comments yet.

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image