164k views
2 votes
I am having problems getting the correct output. Here are the instructions:

For this assignment, you will write a program that will accept up to 2048 characters from stdin
and emit a report to stdout listing each unique word found in the input, the offset of the
beginning of the word from the beginning of the input, the number of times the word was in the
input and the word itself. You should store the input characters into a character array as your
working buffer.

1 Answer

6 votes

Final answer:

To solve the assignment, create a character array as a working buffer and follow the steps to tokenize the input, count unique words, and print the report.

Step-by-step explanation:

Program for Listing Unique Words

To solve this assignment, you can follow the steps below:


  1. Create a character array as a working buffer to store the input characters.

  2. Read up to 2048 characters from stdin and store them in the buffer.

  3. Tokenize the input into words based on whitespace.

  4. Count the occurrence of each unique word and store the count along with the word itself.

  5. Print the report listing each unique word, its offset from the beginning of the input, and the count of occurrences.

Example:

If the input is 'I am having problems with my program. I hope you can help me.', the program should output:

Word | Offset | Count
---- | ------ | -----
I | 0 | 1
am | 2 | 1
having | 5 | 1
problems | 12 | 1
with | 21 | 1
my | 26 | 1
program | 29 | 2
hope | 37 | 1
you | 42 | 1
can | 46 | 1
help | 50 | 1
me | 55 | 1

User Davis Peixoto
by
7.2k points