0% found this document useful (0 votes)
34 views

Progam To Convert Ascii To Float Using A Function

This C program contains a function that converts an ASCII string to a float number. The function takes in a character array, loops through the string to extract the sign, integer part, decimal part, and exponent. It then calculates the float value and returns it. The main function prompts the user for input, calls the conversion function, and prints out the float output.

Uploaded by

umesh777raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Progam To Convert Ascii To Float Using A Function

This C program contains a function that converts an ASCII string to a float number. The function takes in a character array, loops through the string to extract the sign, integer part, decimal part, and exponent. It then calculates the float value and returns it. The main function prompts the user for input, calls the conversion function, and prints out the float output.

Uploaded by

umesh777raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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