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
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 |
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 |
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 |
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 |
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 |
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 |
How can I prevent other programmers from violating encapsulation by seeing the private parts of my class?
Where static variables are stored in memory in c?
What is const and volatile in c?
4.weight conversion: Write a program that will read weight in pounds and convert it into grams.print both the original weight and the converted value.There are 454 grams in a pound.design and carry out a test plan for this program.
‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.
What is the function of multilevel pointer in c?
Is main() function predfined or userdefined?
What is else if ladder?
what is the difference between const volatile int i & volatile const int j;
How to receive strings with spaces in scanf()
What is structure packing ?
Write a program to find the given number is odd or even without using any loops(if,for,do,while)