Good day! The problem says to display all numbers from the line. I read the number x by means of atof and "jump over" it with the help of sprintf into the buffer which returns the number of characters in the number x - then I change the position of the pointer. There is a condition to exit the cycle. Tell me, please, what is the root problem of my code, which instead of outputting all real numbers from the entered string is looped through. Is it possible to correct this fragment - or is the idea co sprintf inherently wrong? Thanks for attention.
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> int main(void){ char str[50]; char buffer[50]; double x; puts("enter a string: \n"); fgets(str, 50, stdin); str[strlen(str) - 1] = '\0'; //print all doubles from string; char* strptr = str; while(*strptr != '\0'){ if(isdigit(*strptr)){ x = atof(strptr); printf("%f\n", x); strptr = strptr + (sprintf(buffer, "%f", x)); } else strptr++; } return 0; }