Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5.  
  6. void print_numbers(char *input);
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     if (argc == 1)
  11.     {
  12.         char buf[LINE_MAX+1];
  13.         while (fgets(buf, LINE_MAX, stdin))
  14.             print_numbers(buf);
  15.     }
  16.     else
  17.     {
  18.         for (int k = 1; k < argc; k++)
  19.         {
  20.             char *input = argv[k];
  21.             print_numbers(input);
  22.         }
  23.     }
  24.  
  25.     return 0;
  26. }
  27.  
  28. void print_numbers(char *input)
  29. {
  30.     int len = strlen(input);
  31.     char *buf = malloc((len * sizeof(char)) + 1);
  32.     if (buf)
  33.     {
  34.         int j = 0;
  35.         for (int i = 0; i < len; i++)
  36.         {
  37.             char c = input[i];
  38.             if (c >= 48 && c <= 57)
  39.                 buf[j++] = c;
  40.         }
  41.         buf[j] = '\0';
  42.  
  43.         printf("%s", buf);
  44.         free(buf);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement