-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc28.c
More file actions
30 lines (28 loc) · 692 Bytes
/
c28.c
File metadata and controls
30 lines (28 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<stdio.h>
#include<string.h>
int myAtoi(const char *s)
{
int i=0;//to traverse string
int sign=1;//default positive
int result=0;
while(s[i]==' ')//skip spaces
i++;
while(s[i]=='-'||s[i]=='+')//ensuring sign
{
sign=(s[i]=='-')?-1:1;
i++;
}
while(s[i]>='0'&&s[i]<='9')
{
result=result*10+(s[i]-'0');//doesnt converts char to int and for char like '1' we use s[i]-'0'i.e. 56-55=1;
i++;
}
return sign*result;
}
int main()
{
printf("%d\n", myAtoi(" -1234")); // Output: -1234
printf("%d\n", myAtoi("42")); // Output: 42
printf("%d\n", myAtoi("+99abc")); // Output: 99
return 0;
}