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

Answers were Sorted based on User's Feedback



Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not..

Answer / piyush sharma

#include<iostream>

using namespace std;

int main()
{
char* str = "36.78sg67";
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;

cout << val << endl;
system("pause");
return 0;
}

Is This Answer Correct ?    3 Yes 1 No

Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not..

Answer / 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

Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not..

Answer / nikita

The above two solutions do not take care of the numberes
after the decimal.

for eg; "123.78" would return 123.0000 instead it should
return 123.78.

Is This Answer Correct ?    1 Yes 1 No

Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not..

Answer / 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

Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not..

Answer / fernando

If I have character buffer with many floats and want to
separate it as I do?

Example:

for (uint8_t i = 0; i < n; i++)
{
buf [i]; 25.45, 26.54,...
}
buf[0]=
buf[1]=
.
.
.
buf[n]=

Thanks.

Is This Answer Correct ?    0 Yes 0 No

Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not..

Answer / nikita

#include<stdio.h>
#include<string.h>

int main()
{
int i = 0;
float result = 0;
int dec_count = 0;
int isDec = 0;
char* str = "123.76bg";

while(str[i])
{
if(str[i] == '.')
{
isDec = 1;
i++;
}
if((str[i]>=48) && (str[i]<=57))
{
result = result * 10;
result = result + (str[i++] - '0');
}
else
{
break;
}
if(isDec == 1)
{
dec_count++;
}
}
printf("\nThe result is: %f", result);
for(i=0; i<dec_count; i++)
{
result = result / 10;
}
printf("\nThe result is: %f", result);
return 0;
}

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Interview Questions

Define VARIABLE?

0 Answers   ADP,


Code for calculating square root without using library function, of math.h

4 Answers   IBM,


Is null always equal to 0(zero)?

0 Answers  


How many parameters should a function have?

0 Answers  


Do you know what are bitwise shift operators in c programming?

0 Answers  


implement OR gate without using any bitwise operator.

1 Answers   Alcatel, Wipro,


read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the vlaue after executing this code? a.200 b.1 c.-1 d.50

4 Answers   TCS,


Difference Between embedded software and soft ware?

1 Answers   Bosch,


What is an lvalue and an rvalue?

1 Answers  


can we change the default calling convention in c if yes than how.........?

0 Answers   Aptech,


what is diffrence between linear and binary search in array respect to operators?what kind of operator can be used in both seach methods?

0 Answers  


Explain data types & how many data types supported by c?

0 Answers  


Categories