summaryrefslogtreecommitdiffstats
path: root/1
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-11-18 19:28:50 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-11-18 19:28:50 +0800
commit3244e6968c5af150ca1d85a727533301b349dd0d (patch)
treea80075e4e4f673ceef8ba2824e16faf505f06434 /1
parentdb556f217693098cd62fb025822f9c55fbc35670 (diff)
downloadk&r-exercises-3244e6968c5af150ca1d85a727533301b349dd0d.tar.gz
1.13.
Diffstat (limited to '1')
-rw-r--r--1/13.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/1/13.c b/1/13.c
new file mode 100644
index 0000000..8ecc549
--- /dev/null
+++ b/1/13.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+
+#define MAXWRD 100
+
+/* prints a histogram of lengths of words in the input */
+int main(int argc, char *argv[]) {
+ int i, j, p, c, wc, cc, size;
+ int data[MAXWRD];
+
+ p = '0';
+ cc = wc = 0;
+
+ for (i = 0; i < MAXWRD; i++)
+ data[i] = 0;
+
+ while ((c = getchar()) != EOF) {
+ if (wc >= MAXWRD) {
+ printf("Max word count %d exceeded\n", MAXWRD);
+ return 1;
+ }
+
+ if (c == ' ' || c == '\t' || c == '\n') {
+ if (p != ' ' && p != '\t' && p != '\n') {
+ data[wc++] = cc;
+ cc = 0;
+ }
+ } else {
+ cc++;
+ }
+
+ p = c;
+ }
+
+ for (i = 0; i < MAXWRD; i++) {
+ size = data[i];
+ if (size == 0)
+ break;
+
+ printf("%3d: ", i + 1);
+
+ for (j = 0; j < size; j++) {
+ putchar('x');
+ }
+
+ putchar('\n');
+ }
+
+ return 0;
+} \ No newline at end of file