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

Why we use int main and void main?

0 Answers  


Explain how can you determine the size of an allocated portion of memory?

0 Answers  


Why do we use pointer to pointer in c?

0 Answers  


main() { int x=5; printf("%d %d %d\n",x,x<<2,x>>2); } what is the output?

9 Answers   Ramco,


hw can we delete an internal node of binary search tree the internal node has child node..plz write progarm

0 Answers   TCS,


Look at the Code: main() { int a[]={1,2,3},i; for(i=0;i<3;i++) { printf("%d",*a); a++; } } Which Statement is/are True w.r.t the above code? I.Executes Successfully & Prints the contents of the array II.Gives the Error:Lvalue Required III.The address of the array should not be changed IV.None of the Above. A)Only I B)Only II C)II & III D)IV

5 Answers   Accenture,


code for quick sort?

0 Answers  


write a program to rearrange the array such way that all even elements should come first and next come odd

0 Answers  


Dear Sir, we are required the bubble sorting programs Regs Prem

1 Answers  


If fflush wont work, what can I use to flush input?

0 Answers  


Do you know what is the purpose of 'extern' keyword in a function declaration?

0 Answers  


I just typed in this program, and it is acting strangely. Can you see anything wrong with it?

0 Answers  


Categories