Question

In: Computer Science

Write a C function str_to_float() that takes a numeric string as input and converts it to...

Write a C function str_to_float() that takes a numeric string as input and converts it to a floating
point number. The function should return the floating point number by reference. If the
conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not
contain a valid number). The prototype of the function is given below
int str_to_float(char * str, float * number);

Solutions

Expert Solution

Solution

#include <stdio.h>

int str_to_float(char * str, float * number)
{
int i =0;
float f=0.0;
int res;
while(str[i]!='\0')
{
if(str[i]<48 || str[i]>57)
{
return -1;
}
int x= (int)str[i]-48; /* To get the digit from the character of the string */
f = f*10 + x; /* f is the converted floating point number */
i++;
}
*number=f; /* number is a pointer to float so store f at the address stored in number */
return 0;
}

int main()
{
char string[100];
float num=0.0;
int result;
printf("Enter a numeric string ");
scanf("%s", string);
result = str_to_float(string,&num);
if(result==-1)
{
printf("Entered string was not a valid numeric string");
}
if(result==0)
{
printf("The converted floating point number is %f",num);
}
return 0;
}

Explanation

The ASCII value of digits 0 to 9 is given below:

Digit ASCII Value
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57

So we retrieve a character one by one from the string typecast it to integer(We get the ASCII value in this step). Next convert the ASCII value to numeric digit 0 to 9 by subtracting 48 from the ASCII value.

Screenshot

Output

Example of valid number

Example of invalid number



Related Solutions

Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input...
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input string will be a sentance ex: "The cat is on the chair and the bear is on the chair") -- returns a table (word varchar(max), count int) (Output is a table that shows how many times each word appeared in the sentance) Where each space-delimited “word” in the string appears in the table along with the number of times it appeared in the input...
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT