KnRBookExercises/Chapter1/ex1-13-vertical.c

52 lines
1.1 KiB
C

/*
* Exercise 1-13-vertical. 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>
#define IN 1
#define OUT 0
int len(const char *);
main()
{
int word_count = 0;
/*
int c, state;
state = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
continue;
} else if (state == OUT) {
state = IN;
putchar('\n');
}
if (state == IN) {
putchar('*');
}
}
*/
printf("%s length is %d.\n", "Hello", len("Hello"));
printf("%s length is %d.\n", "", len(""));
printf("%s length is %d.\n", NULL, len(NULL));
}
int len(const char *str)
{
if (str == NULL)
return -1;
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}