I want to write a function in C that has one string parameter and returns a double number.
For example when the string is fsldnf213414fasfa
it should return 213414
.
But it should be also able to return floating points like fasfasf123.412412fasfff
as 123.412412
.
I've already a function that can extract only integer numbers not floating points:
double get_num(const char* s)
{
unsigned int limit = UINT_MAX / 10;
double value = 0;
if ( !s ) {
return 0;
}
for ( ; *s; ++s ) {
if ( value < limit ) {
if ( isdigit(*s) ) {
value *= 10;
value += (*s - '0');
}
}
else {
return UINT_MAX;
}
}
return value;
}