Answer to Question #199301 in C for Lucy

Question #199301

Task 1: Convert a number string to number 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); 


1
Expert's answer
2021-05-27T01:01:37-0400
 #include<stdio.h>
#include<stdlib.h>
int str_to_float(char* str, float* number){
  *number= atof(str);
  if(*number==0)
    return -1;
  else
    return 0;
}
void main(){
char s[200];
float number=0;
int k;
printf("Enter your string ");
scanf("%[^\n]%*c", s);
k=str_to_float(s,&number);
if(k==0){
  printf("convert successfully\n");
  printf("%f",number);
}
else
  printf("not converted\n");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS