This is a atof
function that converts string to float
by first converting the string in to integer then dividing the integer number by 10s to get the real number after saving the deciaml point position
Although the condition is true, the program doesn't enter the second while loop that is responsible for omitting the decimal point
I'm using Geany 1.23.1 on ubuntu14.04
#include <stdio.h>
#include <math.h>
#include <string.h>
double atof1(char num[]); //converts from character to float
// by converting string to integer first then divide by
//10 to the power of the count of fraction part
int main()
{
char test[]="123.456";
printf("%f\n",atof1(test));
return 0;
}
double atof1(char num[])
{
int dp=0; //decimal point position
int length=strlen(num);
int i=0;
while(dp==0) //increment till you find decimal point
{
if(num[i]=='.')
{
dp=i; //decimal point found
break;
}
else i++;
}
printf("%d %d\n",i,length);
while(length>i); //delete the decimal point to get integer number
{
num[i]=num[i+1]; //deletes deicmal point
i++;
printf("%d",i);
}
int integer_number=atoi(num); //integer number of the string
double final =integer_number/(pow(10,(length-dp-1)));//divide by 10s to get the float according to position of the '.'
return final;
}