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 |
write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1
Explain why can’t constant values be used to define an array’s initial size?
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
Input is "rama loves rajesh and rajesh Loves rama also and rajesh wear gloves and bloves" To print output is count the numbers of times repeted the word love without case sensitive.
How to find the usage of memory in a c program
Explain how do you list files in a directory?
Which node is more powerful and can handle local information processing or graphics processing?
what is the meaning of 'c' language
#include<stdio.h> void main() { int a=5,b=6,c; int x=(a<b)+7; int y=(x==7)*9; int z=(c=x+y)*2; printf("%h %h %h",x,y,z); } What is the output? Explain it.
How the processor registers can be used in C ?
write a C program : To find out the number of identical words in two files . the file name should be taken as command line argument .
Is fortran still used in 2018?