forked from KIX/lab1_git
35 lines
613 B
C
35 lines
613 B
C
#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;
|
|
}
|