I’m designing a function that will convert a string into a float.
e.g. “45.5” = 45.5
I have this so far. But it doesn’t seem to work. Keep in mind, we cannot use any C library functions like atoi, atof or even pow for that matter.
int str2float( char *s )
{
int num = 0;
int dec = 0;
double i = 1.0;
int ten = 1;
/***** ADD YOUR CODE HERE *****/
for(; *s != '\0'; s++)
{
if (*s == '.'){
for(; *s != '\0'; s++){
dec = (dec * CONT) + (*s - '0');
i++;
}
}else{
num = (num * CONT) + (*s - '0');
}
}
for(;i!=0;i--){
ten *= 10;
}
dec = dec / (ten);
printf("%d", dec);
num += dec;
return num;
}
Here is my try: