Extract Two Float/Double Numbers Out of a String on C?
Go to solution
Solved by TukangUsapEmenq,
10 hours ago, Unimportant said:#include <stdio.h> int main(void) { const char* exampleString = "-2.2+3i"; double real; double im; if (sscanf(exampleString, "%*[^0123456789]%lf%*[^0123456789]%lf", &real, &im) != 2) { printf("Failed to extract real & im!\n"); return 1; } printf("real: %lf, im: %lf\n", real, im); return 0; }
Exactly what I need. Not yet functioning as intended, but thanks a lot!
EDIT: got it works. Might be useful for another beginners around here.
#include <stdio.h> int main() { double real; double im; char exampleString[20]; scanf("%s",&exampleString); sscanf(exampleString, "%lf+%lf", &real, &im); printf("real: %f, im: %f\n", real, im); return 0; }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now