Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define STR_LEN 100
- /*
- Count specific characters in string.
- whatToCount:
- 1 - spaces
- 2 - new lines
- 3 - tabs
- */
- int countChars(char *s, int whatToCount) {
- int i, count = 0;
- for (i = 0; s[i] != '\0'; i++) {
- if (whatToCount == 1 && s[i] == ' ')
- count++;
- else if (whatToCount == 2 && s[i] == '\n')
- count++;
- else if (whatToCount == 3 && s[i] == '\t')
- count++;
- }
- return count;
- }
- int main() {
- char s[STR_LEN], c;
- int i = 0;
- while ((c = getchar()) != EOF)
- s[i++] = c;
- s[i] = '\0';
- printf("%d spaces, %d new lines, %d tabs\n",
- countChars(s, 1),
- countChars(s, 2),
- countChars(s, 3));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment