#include <stdio.h> // for getchar(), putchar(), printf(), EOF
// counts digits, whitespaces, other characters
int main()
{
int c, i;
int nwhite = 0, nother = 0;
int ndigit[10];
for (i = 0; i < 10; i++)
{ndigit[i] = 0;}
while ((c = getchar()) != EOF)
{
if (c >= '0' && c <= '9')
{++ndigit[c-'0'];}
else if (c == ' ' || c == '\t' || c == '\n')
{++nwhite;}
else {++nother;}
}
printf("digits = \t");
for (i = 0; i < 10; i++)
{printf("%3d", i);}
putchar('\n');
putchar('\t');putchar('\t');
for (i = 0; i < 10; i++)
{printf("---");}
putchar('\n');
printf("occurrences = \t");
for (i = 0; i < 10; i++)
{printf("%3d", ndigit[i]);}
putchar('\n');
printf("whitespaces = \t%d\n", nwhite);
printf("others = \t%d\n", nother);
}
/*
gcc countc.c -o countc
./countc // input from keyboard
Hello!
0 1 2 3 4 5 6 7 8 9
digits = 0 1 2 3 4 5 6 7 8 9
------------------------------
occurrences = 1 1 1 1 1 1 1 1 1 1
whitespaces = 11 // 9 blanks, 2 newlines
others = 6
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
./countc < countc.c // input from source file
digits = 0 1 2 3 4 5 6 7 8 9
------------------------------
occurrences = 18 31 5 8 7 6 13 11 8 9
whitespaces = 514
others = 964
./countc < countc // input from binary file
digits = 0 1 2 3 4 5 6 7 8 9
------------------------------
occurrences = 16 7 16 5 8 8 7 1 17 6
whitespaces = 81
others = 16668
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#define MAXLENGTHS 1000 // max no of different word lengths
#define FALSE 0
#define TRUE 1
// histogram of lengths of words, horizontally
int main()
{
int c, i, j;
int wlengths[MAXLENGTHS]; // keep unsorted
int occurrences[MAXLENGTHS]; // match wlengths[]
int wl = 0; // current word length
int nwl = 0; // no of different word lengths
int newlength = FALSE; // new word length
for (i = 0; i < MAXLENGTHS; i++) // initialize
{occurrences[i] = wlengths[i] = 0;}
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v')
{
if (wl > 0) // new word
{
newlength = TRUE;
for (i = 0; i < nwl; i++)
{
if (wl == wlengths[i])
{
occurrences[i]++;
newlength = FALSE;
break; // out of for()
}
}
if (newlength == TRUE)
{
newlength = FALSE; // reset newlength
wlengths[nwl] = wl; // word length
occurrences[nwl]++; // occurrences[nwl] = 1;
nwl++; // array index starts at 0, initial value of nwl
}
wl = 0; // reset current word length
}
// else skip whitespace
}
else {wl++;} // not whitespace
}
printf("\t\tLEGEND:\n");
printf("Lengths:\t");
for (i = 0; i < nwl; i++) // print until wlengths[i] == 0
{printf("%d ",wlengths[i]);}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < nwl; i++)
{printf("%d ",occurrences[i]);}
putchar('\n');
printf("\t\tHISTOGRAM:\n");
printf("Lengths:\tOccurrences:\n");
for (i = 0; i < nwl; i++)
{
printf("%8d\t",wlengths[i]); // 8 is the length of "Lengths:"
for (j = 0; j < occurrences[i]; j++)
{putchar('-');}
putchar('\n');
}
}
/*
gcc histwh.c -o histwh
./histwh < histwh.c // input source file
LEGEND:
Lengths: 8 9 7 10 4 2 3 5 1 6 12 21 24 11 15 17 13 19 16 14 25 27 35 28
Occurrences: 6 9 10 8 30 78 27 16 60 17 2 2 2 5 3 1 1 2 2 3 1 1 1 1
HISTOGRAM:
Lengths: Occurrences:
8 ------
9 ---------
7 ----------
10 --------
...................
./histwh < histwh // input binary file
LEGEND:
Lengths: 56 240 7 174 103 24 282 19 67 123 23 270 15 168 2594 84 49 107 63 145 8 1 6 257 11 36 34 12 45 40 65 41 2815 16 3 13 88 54 86 4 32 31 3130 127 315 233 120 206 344 44 69 215 52 22 75 1091 99 459 263 48 55 358 191
Occurrences: 1 1 11 1 1 1 1 3 1 1 10 1 3 1 1 1 1 1 2 1 5 3 1 1 2 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1
HISTOGRAM:
Lengths: Occurrences:
56 -
240 -
7 -----------
174 -
103 -
24 -
282 -
19 ---
168 -
2594 -
84 -
................
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#define MAXLENGTHS 1000 // max no of different word lengths
#define FALSE 0
#define TRUE 1
// histogram of lengths of words, vertically
int main()
{
int c, i, j;
int wlengths[MAXLENGTHS]; // keep unsorted
int occurrences[MAXLENGTHS]; // match wlengths[]
int maxocc = 0; // max no of occurrences of a length
int wl = 0; // current word length
int nwl = 0; // no of different word lengths
int newlength = FALSE; // new word length
for (i = 0; i < MAXLENGTHS; i++) // initialize
{occurrences[i] = wlengths[i] = 0;}
while ((c = getchar()) != EOF)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v')
{
if (wl > 0) // new word
{
newlength = TRUE;
for (i = 0; i < nwl; i++)
{
if (wl == wlengths[i])
{
occurrences[i]++;
if (occurrences[i] > maxocc)
{maxocc = occurrences[i];}
newlength = FALSE;
break; // out of for()
}
}
if (newlength == TRUE)
{
newlength = FALSE; // reset newlength
wlengths[nwl] = wl; // word length
occurrences[nwl]++; // occurrences[nwl] = 1;
if (occurrences[nwl] > maxocc) // if (maxocc == 0)
{maxocc = occurrences[nwl];} // {maxocc = 0;}
nwl++; // array index starts at 0, initial value of nwl
}
wl = 0; // reset current word length
}
// else skip whitespace
}
else {wl++;} // not whitespace
}
printf("\t\tLEGEND:\n");
printf("Lengths:\t");
for (i = 0; i < nwl; i++) // print until wlengths[i] == 0
{printf("%d ",wlengths[i]);}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < nwl; i++)
{printf("%d ",occurrences[i]);}
putchar('\n');
printf("\t\tHISTOGRAM:\n");
printf("Occ.:\t\\\tLengths:\n");
for (i = 0; i < nwl; i++)
{printf("%d ",wlengths[i]);}
putchar('\n');
for (i = 0; i < maxocc; i++)
{
for (j = 0; j < nwl; j++)
{
if(occurrences[j] > i)
{putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
}
/*
gcc histwv.c -o histwv
./histwv < histwv.c // input source file
LEGEND:
Lengths: 8 9 7 10 4 2 3 5 1 6 21 24 11 15 12 17 16 13 19 18 14 25 27 32 23
Occurrences: 6 9 17 9 35 91 30 16 72 19 2 2 7 2 2 3 4 1 2 1 4 1 1 1 1
HISTOGRAM:
Occ.: \ Lengths:
8 9 7 10 4 2 3 5 1 6 21 24 11 15 12 17 16 13 19 18 14 25 27 32 23
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | |
..............................................
./histwv < histwv // input binary file
LEGEND:
Lengths: 56 240 7 174 103 24 228 53 19 67 123 23 270 15 168 2594 84 49 107 63 155 8 1 6 349 11 45 34 12 40 66 9 4 25 2620 16 3 13 98 54 75 10 32 31 3138 127 315 233 120 206 344 44 69 215 52 86 39 35 1091 99 459 263 48 55 358 191
Occurrences: 1 1 11 1 1 1 1 1 2 1 1 10 1 3 1 1 1 1 1 1 1 4 4 1 1 3 3 3 2 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1
HISTOGRAM:
Occ.: \ Lengths:
56 240 7 174 103 24 228 53 19 67 123 23 270 15 168 2594 84 49 107 63 155 8 1 6 349 11 45 34 12 40 66 9 4 25 2620 16 3 13 98 54 75 10 32 31 3138 127 315 233 120 206 344 44 69 215 52 86 39 35 1091 99 459 263 48 55 358 191
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | |
...................................
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
// histogram of frequencies of different characters, horizontally
int main()
{
int c, i, j;
int digits[10]; // 0..9
int lower[26]; // a..z
int upper[26]; // A..Z
int white[6]; // whitespace: ' ', '\t', '\n', '\f', '\r', '\v'
for (i = 0; i < 10; i++) // initialize
{digits[i] = 0;} // initially, 0 occurrences
for (i = 0; i < 26; i++) // initialize
{lower[i] = upper[i] = 0;}
for (i = 0; i < 6; i++) // initialize
{white[i] = 0;}
while ((c = getchar()) != EOF)
{
if (c == ' ') {white[0]++;}
else if (c == '\t') {white[1]++;}
else if (c == '\n') {white[2]++;}
else if (c == '\f') {white[3]++;}
else if (c == '\r') {white[4]++;}
else if (c == '\v') {white[5]++;}
else if (c >= '0' && c <= '9')
{digits[c-'0']++;}
else if (c >= 'A' && c <= 'Z')
{upper[c-'A']++;}
else if (c >= 'a' && c <= 'z')
{lower[c-'a']++;}
}
printf("\t\tLEGEND:\n");
printf("Spaces: %d ", white[0]);
printf("Tabs: %d ", white[1]);
printf("Newlines: %d ", white[2]);
printf("Form feeds: %d ", white[3]);
printf("Returns: %d ", white[4]);
printf("Vertical tabs: %d ", white[5]);
putchar('\n');
printf("Digits:\t\t");
for (i = 0; i <= 9; i++)
{printf("%d ",i);}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i <= 9; i++)
{printf("%d ",digits[i]);}
putchar('\n');
printf("Lowercase:\t");
for (i = 'a'; i <= 'z'; i++)
{putchar(i);putchar(' ');}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < 26; i++)
{printf("%d ",lower[i]);}
putchar('\n');
printf("Uppercase:\t");
for (i = 'A'; i <= 'Z'; i++)
{putchar(i);putchar(' ');}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < 26; i++)
{printf("%d ",upper[i]);}
putchar('\n');
putchar('\n');
printf("\t\tHISTOGRAM:\n");
printf("Spaces:\t\t");
for (i = 0; i < white[0]; i++)
{putchar('-');}
putchar('\n');
printf("Tabs:\t\t");
for (i = 0; i < white[1]; i++)
{putchar('-');}
putchar('\n');
printf("Newlines:\t");
for (i = 0; i < white[2]; i++)
{putchar('-');}
putchar('\n');
printf("Form feeds:\t");
for (i = 0; i < white[3]; i++)
{putchar('-');}
putchar('\n');
printf("Returns:\t");
for (i = 0; i < white[4]; i++)
{putchar('-');}
putchar('\n');
printf("Vertical tabs:\t");
for (i = 0; i < white[5]; i++)
{putchar('-');}
putchar('\n');
printf("Digits:\n");
for (i = 0; i <= 9; i++)
{
printf("%d\t",i);
for (j = 0; j < digits[i]; j++)
{putchar('-');}
putchar('\n');
}
printf("Lowercase letters:\n");
for (i = 0; i < 26; i++)
{
putchar('a'+i);printf("\t");
for (j = 0; j < lower[i]; j++)
{putchar('-');}
putchar('\n');
}
printf("Uppercase letters:\n");
for (i = 0; i < 26; i++)
{
putchar('A'+i);printf("\t");
for (j = 0; j < upper[i]; j++)
{putchar('-');}
putchar('\n');
}
}
/*
gcc histch.c -o histch
./histch < histch.c // input source file
LEGEND:
Spaces: 604 Tabs: 0 Newlines: 134 Form feeds: 0 Returns: 0 Vertical tabs: 0
Digits: 0 1 2 3 4 5 6 7 8 9
Occurrences: 31 5 10 3 3 3 9 0 0 5
Lowercase: a b c d e f g h i j k l m n o p q r s t u v w x y z
Occurrences: 61 5 86 21 97 71 10 71 189 10 0 30 4 80 39 81 1 124 49 148 48 2 31 0 3 7
Uppercase: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Occurrences: 6 0 0 3 3 3 2 1 1 0 0 3 1 3 5 0 0 3 3 3 2 2 0 0 0 3
HISTOGRAM:
Spaces: ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Tabs:
Newlines: --------------------------------------------------------------------------------------------------------------------------------------
Form feeds:
Returns:
Vertical tabs:
Digits:
0 -------------------------------
1 -----
2 ----------
3 ---
4 ---
5 ---
6 ---------
7
8
9 -----
Lowercase letters:
a -------------------------------------------------------------
b -----
................
z -------
Uppercase letters:
A ------
B
................
Z ---
./histch < histch // input binary file
LEGEND:
Spaces: 57 Tabs: 37 Newlines: 30 Form feeds: 12 Returns: 9 Vertical tabs: 18
Digits: 0 1 2 3 4 5 6 7 8 9
Occurrences: 17 8 19 2 8 9 8 2 17 16
Lowercase: a b c d e f g h i j k l m n o p q r s t u v w x y z
Occurrences: 79 26 41 45 85 23 20 27 63 0 4 39 26 54 40 20 2 72 63 96 37 2 6 15 18 6
Uppercase: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Occurrences: 22 18 18 27 14 11 20 89 22 2 0 22 15 11 6 6 1 11 9 23 11 4 2 12 2 2
HISTOGRAM:
Spaces: ---------------------------------------------------------
Tabs: -------------------------------------
Newlines: ------------------------------
Form feeds: ------------
Returns: ---------
Vertical tabs: ------------------
Digits:
0 -----------------
1 --------
2 -------------------
3 --
4 --------
5 ---------
6 --------
7 --
8 -----------------
9 ----------------
Lowercase letters:
a -----------...
b --------------------------
.....................
z ------
Uppercase letters:
A ----------------------
B ------------------
.....................
Z --
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
// histogram of frequencies of different characters, vertically
int main()
{
int c, i, j;
int max; // max no of occs of diff char types
int digits[10]; // 0..9
int lower[26]; // a..z
int upper[26]; // A..Z
int white[6]; // whitespace: ' ', '\t', '\n', '\f', '\r', '\v'
for (i = 0; i < 10; i++) // initialize
{digits[i] = 0;} // initially, 0 occurrences
for (i = 0; i < 26; i++) // initialize
{lower[i] = upper[i] = 0;}
for (i = 0; i < 6; i++) // initialize
{white[i] = 0;}
while ((c = getchar()) != EOF)
{
if (c == ' ') {white[0]++;}
else if (c == '\t') {white[1]++;}
else if (c == '\n') {white[2]++;}
else if (c == '\f') {white[3]++;}
else if (c == '\r') {white[4]++;}
else if (c == '\v') {white[5]++;}
else if (c >= '0' && c <= '9')
{digits[c-'0']++;}
else if (c >= 'A' && c <= 'Z')
{upper[c-'A']++;}
else if (c >= 'a' && c <= 'z')
{lower[c-'a']++;}
}
printf("\t\tLEGEND:\n");
printf("Spaces: %d ", white[0]);
printf("Tabs: %d ", white[1]);
printf("Newlines: %d ", white[2]);
printf("Form feeds: %d ", white[3]);
printf("Returns: %d ", white[4]);
printf("Vertical tabs: %d ", white[5]);
putchar('\n');
printf("Digits:\t\t");
for (i = 0; i <= 9; i++)
{printf("%d ",i);}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i <= 9; i++)
{printf("%d ",digits[i]);}
putchar('\n');
printf("Lowercase:\t");
for (i = 'a'; i <= 'z'; i++)
{putchar(i);putchar(' ');}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < 26; i++)
{printf("%d ",lower[i]);}
putchar('\n');
printf("Uppercase:\t");
for (i = 'A'; i <= 'Z'; i++)
{putchar(i);putchar(' ');}
putchar('\n');
printf("Occurrences:\t");
for (i = 0; i < 26; i++)
{printf("%d ",upper[i]);}
putchar('\n');
putchar('\n');
printf("\t\tHISTOGRAM:\n");
/*
printf("Spaces:\tTabs:\tNewlines:\tForm feeds:\tReturns:\tVertical tabs:\n");
max = 0;
for (i = 0; i < 6; i++)
{
if(max < white[i]) {max = white[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < 6; j++)
{
if (white[j] > i) {putchar('|');putchar('\t');}
else {putchar('\t');} // indent
}
putchar('\n');
}
*/
printf("Digits:\n");
for (i = 0; i <= 9; i++) {printf("%d ", i);}
putchar('\n');
max = 0; // reset max
for (i = 0; i <= 9; i++)
{
if(max < digits[i]) {max = digits[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j <= 9; j++)
{
if (digits[j] > i) {putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
printf("Lowercase letters:\n");
for (i = 0; i < 26; i++)
{putchar('a'+i);putchar(' ');}
putchar('\n');
max = 0; // reset max
for (i = 0; i < 26; i++)
{
if(max < lower[i]) {max = lower[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < 26; j++)
{
if (lower[j] > i) {putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
printf("Uppercase letters:\n");
for (i = 0; i < 26; i++)
{putchar('A'+i);putchar(' ');}
putchar('\n');
max = 0; // reset max
for (i = 0; i < 26; i++)
{
if(max < upper[i]) {max = upper[i];}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < 26; j++)
{
if (upper[j] > i) {putchar('|');putchar(' ');}
else {putchar(' ');putchar(' ');} // indent
}
putchar('\n');
}
}
/*
gcc histcv.c -o histcv
./histcv < histcv.c // input source file
LEGEND:
Spaces: 784 Tabs: 0 Newlines: 155 Form feeds: 0 Returns: 0 Vertical tabs: 0
Digits: 0 1 2 3 4 5 6 7 8 9
Occurrences: 37 4 13 2 2 2 15 0 0 7
Lowercase: a b c d e f g h i j k l m n o p q r s t u v w x y z
Occurrences: 89 5 96 28 118 79 12 68 212 17 0 36 25 81 46 85 1 134 60 151 56 9 30 21 4 6
Uppercase: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Occurrences: 6 0 0 3 3 3 2 1 1 0 0 3 1 3 5 0 0 3 3 3 2 2 0 0 0 3
HISTOGRAM:
Digits:
0 1 2 3 4 5 6 7 8 9
| | | | | | | |
| | | | | | | |
| | | | |
...................
Lowercase letters:
a b c d e f g h i j k l m n o p q r s t u v w x y z
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | |
...................................
Uppercase letters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
| | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | |
| |
| |
|
./histcv < histcv // input binary file
LEGEND:
Spaces: 77 Tabs: 28 Newlines: 27 Form feeds: 13 Returns: 9 Vertical tabs: 18
Digits: 0 1 2 3 4 5 6 7 8 9
Occurrences: 18 8 19 4 9 9 8 2 16 12
Lowercase: a b c d e f g h i j k l m n o p q r s t u v w x y z
Occurrences: 75 23 39 42 77 22 20 25 61 0 6 37 24 54 39 19 2 69 57 93 36 3 5 13 18 6
Uppercase: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Occurrences: 22 18 18 29 15 10 19 91 22 2 0 23 14 9 7 5 1 8 7 22 11 2 2 9 2 2
HISTOGRAM:
Digits:
0 1 2 3 4 5 6 7 8 9
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | |
| | | |
....................
Lowercase letters:
a b c d e f g h i j k l m n o p q r s t u v w x y z
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | |
..........................
Uppercase letters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
| | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | |
................................
*/
then hit Enter or the space bar, or save the result to a file:
./histcv < histcv.c > output.txt