myatoi func code
This commit is contained in:
34
atoi.c
Normal file
34
atoi.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int myatoi(char *s, int l);
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
printf("Print your string number\n");
|
||||
char s[100]; scanf("%s", s);
|
||||
printf("Your converted number is %d\n", myatoi(s, strlen(s)));
|
||||
return 0;
|
||||
}
|
||||
int myatoi(char *s, int l)
|
||||
{
|
||||
int result = 0;
|
||||
int is_pos = 1;
|
||||
for(int i=0; i < l; i++)
|
||||
{
|
||||
if((int)s[i]==45)
|
||||
{
|
||||
is_pos = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if((int)s[i] < 48 || (int)s[i] > 57)
|
||||
{
|
||||
printf("Not a number\n");
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
result = result * 10 + ((int)s[i] - 48);
|
||||
}
|
||||
}
|
||||
result = result * is_pos;
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user