Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Write an implementation of “float stringToFloat(char *str).”
The code should be simple, and not require more than the
basic operators (if, for, math operators, etc.).
• Assumptions
• Don’t worry about overflow or underflow
• Stop at the 1st invalid character and return the number
you have converted till then, if the 1st character is
invalid return 0
• Don’t worry about exponential (e.g. 1e10), instead you
should treat ‘e’ as an invalid character
• Write it like real code, e.g. do error checking
• Go though the string only once
• Examples
• “1.23” should return 1.23
• “1a” should return 1
• “a”should return 0

Answer Posted / piyush sharma

float stringToFloat(char *str)
{
char ch;
int decimal_pt = 0;
int dec_count = 0;
float val = 0.0;

for( int i=0; str[i]!='\0'; i++ )
{
ch = str[i];
if( ch == '.' )
{
decimal_pt = 1;
continue;
}

if( !(ch>=48 && ch<=57) )
break;

val = val*10 + ch-48;
if( decimal_pt == 1 )
dec_count++;
}

for( int i=0; i<dec_count; i++ )
val = val/10;

return val;
}

Is This Answer Correct ?    2 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the disadvantages of external storage class?

1021


explain how do you use macro?

1115


What are the usage of pointer in c?

1147


What is extern variable in c with example?

972


How to write c functions that modify head pointer of a linked list?

955


Does c have an equivalent to pascals with statement?

969


exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above

1092


The number of bytes of storage occupied by short, int and long are a) 2, 2 and 4 b) 2, 4 and 4 c) 4, 4 and 4 d) none

1206


What is 2c dna?

1031


What is a loop?

979


Which is the best website to learn c programming?

1054


What's the right way to use errno?

1101


Hi can anyone tell what is a start up code?

2029


what are the advanced features of functions a) function declaration and prototypes b) calling functions by value or by reference c) recursion d) all the above

1096


What is array of pointers to string?

1017