acobzew

sem2-problem3

Mar 13th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define STR_LEN 100
  4.  
  5. /*
  6.     Count specific characters in string.
  7.    
  8.     whatToCount:
  9.     1 - spaces
  10.     2 - new lines
  11.     3 - tabs
  12. */
  13. int countChars(char *s, int whatToCount) {
  14.     int i, count = 0;
  15.     for (i = 0; s[i] != '\0'; i++) {
  16.         if (whatToCount == 1 && s[i] == ' ')
  17.             count++;
  18.         else if (whatToCount == 2 && s[i] == '\n')
  19.             count++;
  20.         else if (whatToCount == 3 && s[i] == '\t')
  21.             count++;
  22.     }
  23.     return count;
  24. }
  25.  
  26. int main() {
  27.     char s[STR_LEN], c;
  28.     int i = 0;
  29.     while ((c = getchar()) != EOF)
  30.         s[i++] = c;
  31.     s[i] = '\0';
  32.     printf("%d spaces, %d new lines, %d tabs\n",
  33.             countChars(s, 1),
  34.             countChars(s, 2),
  35.             countChars(s, 3));
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment