How to convert string with multiple float numbers to float numbers in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	char s[] = "4.5, 98.591, 8981.4 -2.1989 35.6";
    char *pos = s;
	
    while (pos != s + strlen(s)) {
        float f = strtof(pos, &pos);
        while (*pos == ' ' || *pos == ',') {
            pos++;
        }
        printf("%.4f\n", f);
    }
    
	
    return 0;
}	

   
   
   
/*
run:
   
4.5000
98.5910
8981.4004
-2.1989
35.6000

*/

 



answered Jan 2, 2021 by avibootz

Related questions

1 answer 221 views
1 answer 154 views
1 answer 141 views
5 answers 657 views
657 views asked Aug 25, 2019 by avibootz
2 answers 245 views
245 views asked Jan 13, 2019 by avibootz
...