Answer to Question #197802 in C for Hassam

Question #197802

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-26T06:52:39-0400
#include<stdio.h>
#include<stdlib.h>
int str_to_float(char* str, int* number){
  *number= atoi(str);
  if(*number==0)
    return -1;
  else
    return 0;
}
int main(){
char s[200];
printf("Enter your string ");
scanf("%[^\n]%*c", s);
int number=0;
int k=str_to_float(s,&number);
if(k==0){
  printf("convert successfully\n");
  printf("%d",number);
}
else
  printf("not converted\n");

return 0;
}

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