Advertisement
nguyenvanquan7826

read array number in line text

Nov 17th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int main(void)
  6. {
  7.     /* khong doc duoc str khi bat dau bang dau tru (-)
  8.     nen ta can them vao truoc no 1 ky tu nao do
  9.     */
  10.     char* str= "-1.223 %^& fsf 0.234 56.65 fsf 9 f";
  11.  
  12.     // khai bao, cap phat bo nho them s de cong them vao dau 1 ky tu
  13.     char* const s = (char*)malloc(sizeof(strlen(str))+1);
  14.  
  15.     // cong them ky tu a
  16.     strcpy(s, "a");
  17.     strcpy(s + strlen(s), str);
  18.     printf("%s\n",s);
  19.  
  20.     // tach lay cac so
  21.     int total_n = 0;
  22.     int n; 
  23.     float i; // cac so can tach
  24.  
  25.     printf("\nday cac so\n");
  26.     while (1 == sscanf(s + total_n, "%*[^0123456789-]%f%n", &i, &n))
  27.     {
  28.         total_n += n;
  29.         printf("%f\n", i);
  30.     }
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement