summaryrefslogtreecommitdiff
path: root/one/count.c
diff options
context:
space:
mode:
authorMichael Hunteman <michael@huntm.net>2023-07-04 17:03:53 -0500
committerMichael Hunteman <michael@huntm.net>2023-07-06 17:23:45 -0500
commitbfce8f0d0d828209ec0bec71371ee94a7ad62d3e (patch)
treebdf49ca788ca1ca030d5b1cccfd0c9dffeb3f69f /one/count.c
Initial commit
Diffstat (limited to 'one/count.c')
-rw-r--r--one/count.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/one/count.c b/one/count.c
new file mode 100644
index 0000000..0f5bb82
--- /dev/null
+++ b/one/count.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+
+/* count digits, white space, and others */
+int
+main()
+{
+ int c, i, nwhite, nother;
+ int ndigit[10];
+ int nchar[26];
+ nwhite = nother = 0;
+
+ for (i = 0; i < 26; ++i) {
+ if (i < 10) {
+ ndigit[i] = 0;
+ }
+ nchar[i] = 0;
+ }
+
+ while ((c = getchar()) != EOF) {
+ if (c >= '0' && c <= '9') {
+ ++ndigit[c - '0'];
+ } else if (c == ' ' || c == '\t' || c == '\n') {
+ ++nwhite;
+ } else if (c >= 'a' && c <= 'z') {
+ ++nchar[c - 'a'];
+ } else {
+ ++nother;
+ }
+ }
+
+ printf("digits =");
+ for (i = 0; i < 10; ++i) {
+ printf(" %d", ndigit[i]);
+ }
+ printf("\nchars =");
+ for (i = 0; i < 26; ++i) {
+ printf(" %d", nchar[i]);
+ }
+ printf("\nwhite space = %d\nother = %d\n", nwhite, nother);
+ return 0;
+}