Finished arrays
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
*
|
||||
* Exercise 1-12. Write a program that prints its input one word per line.
|
||||
*
|
||||
* Author: Kun Deng
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define IN_WORD 1
|
||||
#define OUT_WORD 0
|
||||
#define WORD_LOWER_LIMIT 2
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int c;
|
||||
int state = OUT_WORD;
|
||||
|
||||
printf("Enter a series lines, words, and characters\n");
|
||||
printf("Once complete, enter Ctr-D to quit\n");
|
||||
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
|
||||
if (c == '\n' || c == ' ' || c == '\t')
|
||||
{
|
||||
if (state == IN_WORD)
|
||||
{
|
||||
state = OUT_WORD;
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state == OUT_WORD)
|
||||
{
|
||||
state = IN_WORD;
|
||||
}
|
||||
|
||||
putchar(c);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Author: Kun Deng
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define IN_WORD 1
|
||||
#define OUT_WORD 0
|
||||
#define WORD_LOWER_LIMIT 2
|
||||
#define WORD_ARRAY_LIMIT 2
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int c;
|
||||
int state = OUT_WORD;
|
||||
int word_count = 0;
|
||||
int characters_in_word = 0;
|
||||
int word_lengths[WORD_ARRAY_LIMIT];
|
||||
int largest_characters_word_count = 0;
|
||||
|
||||
printf("Enter %d words\n");
|
||||
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
|
||||
if (c == '\n' || c == ' ' || c == '\t')
|
||||
{
|
||||
if (state == IN_WORD)
|
||||
{
|
||||
state = OUT_WORD;
|
||||
putchar('\n');
|
||||
word_lengths[word_count] = characters_in_word;
|
||||
|
||||
if (largest_characters_word_count < characters_in_word)
|
||||
{
|
||||
largest_characters_word_count = characters_in_word;
|
||||
}
|
||||
|
||||
++word_count;
|
||||
printf("characters in word %d\n", characters_in_word);
|
||||
characters_in_word = 0;
|
||||
}
|
||||
|
||||
if (word_count >= WORD_ARRAY_LIMIT)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state == OUT_WORD)
|
||||
{
|
||||
state = IN_WORD;
|
||||
}
|
||||
|
||||
++characters_in_word;
|
||||
|
||||
putchar(c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf("word count %d\n", word_count);
|
||||
printf("most characters in word %d\n\n", largest_characters_word_count);
|
||||
|
||||
for (int s = largest_characters_word_count; s > 0; --s)
|
||||
{
|
||||
char buf[WORD_ARRAY_LIMIT];
|
||||
|
||||
for (int j = 0; j < WORD_ARRAY_LIMIT; ++j)
|
||||
{
|
||||
if (word_lengths[j] >= s)
|
||||
{
|
||||
buf[j] = '*';
|
||||
printf("%2s", "*");
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
// printf("%s\n", buf);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < WORD_ARRAY_LIMIT; ++i)
|
||||
{
|
||||
printf("%2d", i);
|
||||
}
|
||||
|
||||
printf("\n\nwords and their lengths\n");
|
||||
printf("the results are the index for each word and their respective word length\n");
|
||||
printf("ex. first word (index 0) has a length of 3\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
*
|
||||
* This program counts lines, words, and characters. Words being a sequence of
|
||||
* characters that do not contain any blank spaces, tabs, or new lines
|
||||
*
|
||||
* Author: Kun Deng
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define IN_WORD 1
|
||||
#define OUT_WORD 0
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int c;
|
||||
int new_line_count = 0;
|
||||
int word_count = 0;
|
||||
int character_count = 0;
|
||||
int state = OUT_WORD;
|
||||
|
||||
printf("Enter a series lines, words, and characters\n");
|
||||
printf("Once complete, enter Ctr-D to quit\n");
|
||||
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
++character_count;
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
++new_line_count;
|
||||
}
|
||||
|
||||
if (c == '\n' || c == ' ' || c == '\t')
|
||||
{
|
||||
state = OUT_WORD;
|
||||
printf("out of word\n");
|
||||
|
||||
}
|
||||
else if (state == OUT_WORD)
|
||||
{
|
||||
state = IN_WORD;
|
||||
++word_count;
|
||||
printf("in word\n");
|
||||
}
|
||||
}
|
||||
|
||||
printf("character count %d word count %d new line count %d\n", character_count, word_count, new_line_count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user