Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

/*Progam to Convert Ascii to Float using a function*/

#include<stdio.h>
float asctof(char arr[21])
{
int i=0, c, sign1=1, e=0;
float sum=0.0;
for(i=0;isspace(arr[i]);i++);
c = arr[i];
if (c == '+')
c = arr[i++];
else if (c == '-') {
c = arr[i++];
sign1 = -1;
}
while ((c = arr[i++]) != '\0' && isdigit(c)) {
sum = sum*10.0 + (c - '0');
}
if (c == '.') {
while ((c = arr[i++]) != '\0' && isdigit(c)) {
sum = sum*10.0 + (c - '0');
e = e-1;
}
}
if (c == 'e' || c == 'E') {
int sign = 1;

int j = 0;
c = arr[i++];
if (c == '+')
c = arr[i++];
else if (c == '-') {
c = arr[i++];
sign = -1;
}
while (isdigit(c)) {
j = j*10 + (c - '0');
c = arr[i++];
}
e += j*sign;

}
while (e > 0) {
sum *= 10.0;
e--;
}
while (e < 0) {
sum *= 0.1;
e++;
}
sum=sum*sign1;

return sum;
}
main()
{
char arr[21];
float x;
printf("Enter your string:\t");
gets(arr);
x=asctof(arr);
printf("Output: %f\n",x);
}

You might also like