atof:- double my_atof(const char *s) function converts the string argument str to a floating-point number
of (type double).
atoi:- int my_atoi(const char *s) function converts the string argument str to a number of type integer.
#include <stdio.h> #include <string.h> int find_pos(char *); int my_atoi(const char *); double my_atof(const char *); int main() { int i; char s[20]={0,}; double d; printf("Enter the float string : "); gets(s); for(i=0;s[i];i++) { if(isalpha(s[i])) { printf("Invalid data\n"); return 0; } } d = my_atof(s); printf("Floating point Number = %lf\n",d); return 0; } double my_atof(const char *s) { float f,f1,f2; int b,m,i,j,k; char s1[10]={0,},s2[10]={0,}; m = find_pos(s); j = k = 0; for(i=0;i<strlen(s);i++) { if (i != m) { if(i<m) s1[j++]=s[i]; else s2[k++]=s[i]; } } s1[j]='\0'; s2[k]='\0'; for(b=1,i=0;i<strlen(s2);i++,b=b*10); f1 = (float)my_atoi(s1); f2 = (float)my_atoi(s2); f=f1+(f2/b); return (double)f; } int find_pos(char *s) { int i; for(i=0;i<strlen(s);i++) { if (s[i] == '.') return i; } } int my_atoi(const char *s) { int n=0; while(*s) { n = 10*n+((*s)-48); s++; } return n; }
Output
Enter the float string : 44 Floating point Number = 44.000000
Enter the float string : wr45 Invalid data