#include <stdio.h>
#include <stdint.h>
float custom_atof(const char *str) {
// Your logic here
//float res = 0.0;
int i = 0;
float num = 0.0 ;
float num_decimal = 0.0 ;
int flag = 0;
int decimal_flag = 0;
if (str[0] == '-' || str[0] == '+' )
{
i = 1;
}
while(str[i] != '\0')
{
if (str[i] >= '0' && str[i] <= '9' && flag == 0)
{
num = (num * 10 ) + ( int(str[i]) - '0' );
/// printf("%d" , num);
}
else if (str[i] >= '0' && str[i] <= '9' && flag == 1 && decimal_flag < 2)
{
// 123.45
num_decimal = (num_decimal * 10 ) + ( int(str[i]) - '0' ) ;
decimal_flag ++;
}
else if (str[i] == '.')
{
flag = 1;
}
else
{
break;
}
i ++;
}
if (str[0] == '-')
{
return 0 - (num + ((num_decimal)/ 100));
}
return num + ((num_decimal)/ 100);
;
}
int main() {
char str[101];
fgets(str, sizeof(str), stdin);
// Remove newline
uint8_t i = 0;
while (str[i]) {
if (str[i] == '\n') {
str[i] = '\0';
break;
}
i++;
}
float value = custom_atof(str);
printf("%.2f", value);
return 0;
}
Input
123.45
Expected Output
123.45