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 / c l

float stringToFloat(char *str)
{
float retVal = 0;
float devisor = 1;
int strSize = sizeof(str);
int multiplySign = 1;
bool foundDecimal = false;

/* check for non empty char array */
if (strSize > 0 )
{
if (str[0] >= '0' && str[0] <= '9')
retVal = str[0] - '0';

else if (str[0] == '.')
foundDecimal = true;

else if (str[0] == '-')
mulitplySign = -1;

else if (str[0] == '+')
; /* NOP */

else
return retVal;

for (i = 1; i < strSize; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
retValue = retValue*10 + str[i] - '0';
if (foundDecimal)
divisor = divisor * 10;
}
else if (str[i] == '.')
if(foundDecimal) /* 2nd '.', err */
break;
else
foundDecimal = true;

else /* anything else is err cond */
break;
}

return multiplySign * retVal / divisor;
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Differentiate between #include<...> and #include '...'

1003


Can an array be an Ivalue?

1052


What’s a signal? Explain what do I use signals for?

1051


FILE *fp1,*fp2; fp1=fopen("one","w") fp2=fopen("one","w") fputc('A',fp1) fputc('B',fp2) fclose(fp1) fclose(fp2)} a.error b. c. d.

1704


Explain null pointer.

1039


What is #line in c?

959


How do you print only part of a string?

964


What is the value of a[3] if integer a[] = {5,4,3,2,1}?

1029


Is c weakly typed?

966


"%u" unsigned integer print the a) address of variable b) value of variable c) name of a variable d) none of the above

978


When is a null pointer used?

1061


What is the difference between union and anonymous union?

1247


How do I read the arrow keys? What about function keys?

1016


What is difference between union All statement and Union?

1058


How can I list all of the predefined identifiers?

927