Wednesday, February 23, 2011

WAP for Converting String in to Integer it means you have to implement your own atoi function

so Here we Go its Simple Logic

#include

int main(int argc, char* argv[])
{
printf("\n%d\n", myatoi("1998"));
getch();
return(0);
}

int myatoi(const char *string)
{
int i;
i=0;
while(*string)
{
i=(i<<3) + (i<<1) + (*string - '0');
string++;

//i am using left shift which is equals to multiply by pow(2,n) & shifting is fast then multiplication
// Don't increment i!

}
return(i);
}

Run Herehttps://ideone.com/XUKdL

No comments :