Question
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
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.
A word is defined as any sequence of non-whitespace characters. The input will terminate with an EOF if there are less than 2048 characters, but your program shall accept no more than 2048.
Your program shall record the beginning and end of each word in the buffer and use the buffer itself as the storage for the content of each word. The report needs to be generated from the buffer and the index to the words in the buffer.
You are not allowed to declare or use any global variables. All variables must be locally scoped. You may however add any additional #define directives you deem needed or helpful.
Here is my code so far:
.......................................................................................................................
#include
#include
#include "string_index.h"
int main(void) {
// variable declartaions
char buffer[BUFFER_SIZE]; // begins 2048 buffer
int word_index_array[WORD_LIMIT][2]; // index by array
int word_counts[WORD_LIMIT]; // number of occurances
const char *word_index[WORD_LIMIT][2]; // index by pointer
int words_found; // shows the words found
// initialize storage
for (int i = 0; i < BUFFER_SIZE; i++) {
buffer[i] = '\0';
}
/* ***** DO NOT CHANGE ANY CODE BEYOND THIS POINT IN main() ****** */
initialize_buffer(buffer);
words_found = index_buffer_by_ptr(buffer, word_index, word_counts);
print_report_by_ptr(buffer, word_index, word_counts, words_found);
words_found = index_buffer_by_array(buffer, word_index_array, word_counts);
print_report_by_array(buffer, word_index_array, word_counts, words_found);
} // end main
/* ***** YOUR FUNNCTION DEFINTIONS BELOW ***** */
int initialize_buffer(char buffer[]) {
int i = 0;
int c;
while (i < BUFFER_SIZE - 1 && (c = getchar()) != EOF) {
buffer[i++] = c;
}
buffer[i] = '\0';
if (i == 0) {
return -1;
} else {
return i;
}
};
int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] ) {
int word_cnt = 0;
int len = strlen(buffer);
int i = 0;
int word_beg = 0;
int word_end = 0;
int is_word = 0;
for (i = 0; i < len; i++) {
if (!is_word && !isspace(buffer[i])) {
word_beg = i;
is_word = 1;
} else if (is_word && isspace(buffer[i])) {
word_end = i - 1;
is_word = 0;
int j;
for (j = 0; j < word_cnt; j++) {
if (word_end - word_beg != index[j][1] - index[j][0]) {
continue;
}
int k;
for (k = index[j][0]; k <= index[j][1]; k++) {
if (buffer[word_beg + k - index[j][0]] != buffer[k]) {
break;
}
}
if (k == index[j][1] + 1) {
w_counts[j]++;
break;
}
}
if (j == word_cnt) {
index[j][0] = word_beg;
index[j][1] = word_end;
w_counts[word_cnt] = 1;
word_cnt++;
}
}
}
return word_cnt;
};
int find_word_by_array(int word_beg, const char buf[], int index[][2]) {
for (int i = 0; index[i][WD_BGN] != -1; i++) {
if (strncmp(&buf[word_beg], &buf[index[i][WD_BGN]], index[i][WD_END] - index[i][WD_BGN] + 1) == 0) {
return i;
}
}
return NOT_FOUND;
};
void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt) {
for (int i = 0; i < word_cnt; i++) {
printf("%d(%2d): %d %.*s ", i, index[i][WD_BGN], counts[i], index[i][WD_END] - index[i][WD_BGN] + 1, &buf[index[i][WD_BGN]]);
}
};
int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] ) {
int i = 0;
int w_count = 0;
while (*buf != '\0') {
if (!isspace(*buf)) {
// Found a word
const char *word_beg = buf;
while (!isspace(*buf) && *buf != '\0') {
buf++;
}
const char *word_end = buf - 1;
int index_found = find_word_by_ptr(word_beg, index);
if (index_found == NOT_FOUND) {
// New word found
index[w_count][WD_BGN] = word_beg;
index[w_count][WD_END] = word_end;
word_counts[w_count] = 1;
w_count++;
} else {
// Existing word found
word_counts[index_found]++;
}
} else {
buf++;
}
}
return w_count;
};
int find_word_by_ptr(const char *beg, const char *index[][2]) { int i, j; for (i = 0; index[i][WD_END] != NULL; i++) { if (index[i][WD_BGN] == NULL) { continue; } if (strlen(beg) != index[i][WD_END] - index[i][WD_BGN]) { continue; } for (j = 0; j < index[i][WD_END] - index[i][WD_BGN]; j++) { if (*(beg + j) != *(index[i][WD_BGN] + j)) { break; } } if (j == index[i][WD_END] - index[i][WD_BGN]) { return i; } } return NOT_FOUND; };
void print_report_by_ptr(const char *buf, const char *index[][2], int counts[], int word_cnt) {
for (int i = 0; i < word_cnt; i++) {
printf("%d(%2d): %d %.*s ", i, (int)(index[i][WD_BGN] - buf), counts[i], (int)(index[i][WD_END] - index[i][WD_BGN] + 1), index[i][WD_BGN]);
}
};
.........................................................................................................................
Ande here is the header file:
..........................................................................................................................
#include
#include
#ifndef STRING_INDEX_HEADER
#define STRING_INDEX_HEADER
#define BUFFER_SIZE 2048
#define WORD_LIMIT 100
#define NOT_FOUND -1
#define WD_BGN 0
#define WD_END 1
// Prototypes
/*
initialize_buffer:
Fills the given array with characters from stdin until either EOF or
2048 characters have been loaded.
Returns the total number of characters loaded and -1 if no characters loaded.
*/
int initialize_buffer(char buffer[]);
/********************************************************
Functions that work with arrays
*******************************************************
*/
/*
index_buffer_by_array:
Indexes the beginning and end of each unique word (string of non-whitespace characters).
The index is a list of beginning and ending array indexes for each unique word found.
Duplicate words are counted in w_counts array.
Returns the total number of unique words found in the character buffer.
*/
int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] );
/*
find_word_by_array:
Determines if the word in buf[] beginning at index word_beg is already indexed.
A word terminates with the first whitespace character.
Returns the index number for the word if found, otherwise returns NOT_FOUND.
*/
int find_word_by_array(int word_beg, const char buf[], int index[][2]);
/*
print_report_by_array:
Prints to stdout a report giving the word index number, offset of first letter from
beginning of the buffer, the word count, and the word itself. This function expects index
to be an array of array indexes for the beginning and end of each word.
Example output for buffer containing the characters in quotes "all the all"
0( 0): 2 all
1( 4): 1 the
*/
void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt);
/*******************************************************
Functions that work with pointers
********************************************************/
/*
index_buffer_by_ptr:
Indexes the beginning and end of each unique word (string of non-whitespace characters).
The index is a list of beginning and ending char pointers for each unique word found.
Duplicate words are counted in w_counts array.
Returns the total number of unique words found in the character buffer.
*/
int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] );
/*
find_word_by_ptr:
Determines if the word in beginning at the char * beg is already indexed.
A word terminates with the first whitespace character.
Returns the index number for the word if found, otherwise returns NOT_FOUND.
*/
int find_word_by_ptr(const char * beg, const char * index[][2]);
/*
print_report_by_ptr:
Prints to stdout a report giving the word index number, offset of first letter from
beginning of the buffer, the word count, and the word itself. This function expects index
to be an array of char pointers for the beginning and end of each word.
Example output for buffer containing the characters in quotes "all the all"
0( 0): 2 all
1( 4): 1 the
*/
void print_report_by_ptr(const char * buf, const char * index[][2], int counts[], int word_cnt);
#endif
...........................................................................................................................
Here is my current output (NOT WHAT I AM LOOKING FOR):
0( 0): 1 How 1( 4): 1 many 2( 9): 1 wood 3(14): 1 chucks 4(21): 1 would 5(27): 1 chuck 6(33): 1 wood 7(38): 1 if 8(41): 1 a 9(43): 1 wood 10(48): 1 chuck 11(54): 1 could 12(60): 1 chuck 13(66): 1 wood. 0( 0): 1 How 1( 4): 1 many 2( 9): 3 wood 3(14): 1 chucks 4(21): 1 would 5(27): 3 chuck 6(38): 1 if 7(41): 1 a 8(54): 1 could
My array functions almost works, it is just cutting off the last word of the input and it counts words with an uppercase letter as different than a lowercase (e.g., 0(0) 1 All, 1(4) 1 all). Everything else about that is correct. My ptr functions are not working correctly. I am trying to show each unique word once with the count next to them. We are not using c string in this so don't use 'strcmp' please.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started