fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. char * inputdata = "4273 Багров Д. С. 5454 знззз";
  5. // variables to receive the scanned data
  6. int firstint, secondint;
  7. char firststring[32];
  8. char secondstring[32];
  9. char thirdstring[32];
  10. char fourthstring[32];
  11. // important, you should check whether the number of converted elements
  12. // matches what you expect:
  13. int scannedelements;
  14. // let's scan the input
  15. scannedelements = sscanf (inputdata,"%d %s %s %s %d %s",&firstint, firststring, secondstring,
  16. thirdstring,&secondint,fourthstring);
  17. // and show what we found. Notice the similarity between scanf and printf
  18. // but also note the subtle differences!!!
  19. printf("We scanned %d %s %s %s %d %s\n",firstint, firststring, secondstring,
  20. thirdstring,secondint,fourthstring);
  21. printf("That's a total of %d elements\n",scannedelements);
  22. // Alternatively, let's scan the group of 3 strings into 1 variable
  23. scannedelements = sscanf (inputdata,"%d %[^0-9] %d %s",&firstint, firststring, &secondint,fourthstring);
  24. // and show what we found.
  25. printf("We scanned %d %s %d %s\n",firstint, firststring,secondint,fourthstring);
  26. printf("That's a total of %d elements\n",scannedelements);
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 6 elements
We scanned 4273 Багров Д. С.  5454 знззз
That's a total of 4 elements