A
arnuld
this is a programme that counts the "lengths" of each word and then
prints that many of stars(*) on the output . it is a modified form of
K&R2 exercise 1-13. the programme runs without any compile-error BUT
it has a semantic BUG:
what i WANT:
I want it to produce a "horizontal histogram" which tells how many
characters were in the 1st word, how many characters were in the
second word by writing equal number of stars, *, at the output. i
want to
discard ant newlines, extra spaces (more than 1 space between the
words), tabs an newlines.
what i GOT:
it prints histogram BUT it also prints each extra space, tab and
newline as a
"newline in output".
DIFFERENCE: each extra space, tab and newline appears in the output
which must not be there.
---------- PROGRAMME -------
Method:
1.) we will store length of each word in an array.
2.) for keeping simplicity, array size is 1000, i.e.
it can hold only 1000 words.
3.) after EOF is encountered then a horizontal Hostogram
will be printed
*/
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXWORDS 1000
int main()
{
int c;
int i = 0;
int arr_index = 0;
int j = 0; /* "i,j,count" are general index counters */
int nc = 0; /* length of word or number of characters in a word */
int lwords[MAXWORDS + 1];
int nw = 0; /* number of words */
/* length of each word is stored in this array
and will be printed in the end */
int state = IN;
while( ((c = getchar()) != EOF) && (nw <= MAXWORDS) )
{
++nc;
if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}
if(state == OUT)
{
lwords[arr_index++] = nc;
++nw;
state = IN;
nc = 0;
}
}
printf("---------- printing HISTOGRAM -----------\n");
/* printf("arr_index: %d\n", arr_index); */
for(i = 0; i < arr_index; ++i)
{
for(j = 0; j < lwords; ++j)
putchar('*');
putchar('\n');
}
return 0;
}
-------- OUTPUT ----------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like this
---------- printing HISTOGRAM -----------
****
****
[arch@voodo kr2]$ ./a.out
like this
---------- printing HISTOGRAM -----------
****
****
[arch@voodo kr2]$ ./a.out
like this
---------- printing HISTOGRAM -----------
****
****
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
****
[arch@voodo kr2]$
prints that many of stars(*) on the output . it is a modified form of
K&R2 exercise 1-13. the programme runs without any compile-error BUT
it has a semantic BUG:
what i WANT:
I want it to produce a "horizontal histogram" which tells how many
characters were in the 1st word, how many characters were in the
second word by writing equal number of stars, *, at the output. i
want to
discard ant newlines, extra spaces (more than 1 space between the
words), tabs an newlines.
what i GOT:
it prints histogram BUT it also prints each extra space, tab and
newline as a
"newline in output".
DIFFERENCE: each extra space, tab and newline appears in the output
which must not be there.
---------- PROGRAMME -------
Method:
1.) we will store length of each word in an array.
2.) for keeping simplicity, array size is 1000, i.e.
it can hold only 1000 words.
3.) after EOF is encountered then a horizontal Hostogram
will be printed
*/
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXWORDS 1000
int main()
{
int c;
int i = 0;
int arr_index = 0;
int j = 0; /* "i,j,count" are general index counters */
int nc = 0; /* length of word or number of characters in a word */
int lwords[MAXWORDS + 1];
int nw = 0; /* number of words */
/* length of each word is stored in this array
and will be printed in the end */
int state = IN;
while( ((c = getchar()) != EOF) && (nw <= MAXWORDS) )
{
++nc;
if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}
if(state == OUT)
{
lwords[arr_index++] = nc;
++nw;
state = IN;
nc = 0;
}
}
printf("---------- printing HISTOGRAM -----------\n");
/* printf("arr_index: %d\n", arr_index); */
for(i = 0; i < arr_index; ++i)
{
for(j = 0; j < lwords; ++j)
putchar('*');
putchar('\n');
}
return 0;
}
-------- OUTPUT ----------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like this
---------- printing HISTOGRAM -----------
****
****
[arch@voodo kr2]$ ./a.out
like this
---------- printing HISTOGRAM -----------
****
****
[arch@voodo kr2]$ ./a.out
like this
---------- printing HISTOGRAM -----------
****
****
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
****
[arch@voodo kr2]$