fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float str_to_float(char *);
  6. void float_to_str(float,char *);
  7.  
  8. int main(){
  9. int max_size;
  10. float x;
  11. char *arr;
  12. printf("Enter max size of string : ");
  13. scanf("%d",&max_size);
  14. arr=malloc((max_size+1)*sizeof(char));
  15. scanf("%s",arr);
  16. x=str_to_float(arr);
  17. printf("%f\n%f",x,atof(arr));
  18. return 0;
  19. }
  20.  
  21. float str_to_float(char *arr){
  22. int i,j,flag;
  23. float val;
  24. char c;
  25. i=0;
  26. j=0;
  27. val=0;
  28. flag=0;
  29. while ((c = *(arr+i))!='\0'){
  30. // if ((c<'0')||(c>'9')) return 0;
  31. if (c!='.'){
  32. val =(val*10)+(c-'0');
  33. if (flag == 1){
  34. --j;
  35. }
  36. }
  37. if (c=='.'){ if (flag == 1) return 0; flag=1;}
  38. ++i;
  39. }
  40. val = val*pow(10,j);
  41. return val;
  42. }
Success #stdin #stdout 0s 9424KB
stdin
23
1236.965
stdout
Enter max size of string : 1236.964966
1236.965000