Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

displays the occurrence of a character, or a keyword or same-length words asked

ID: 3914966 • Letter: D

Question

displays the occurrence of a character, or a keyword or same-length words asked by the user Your program should implement at least the following functions (in other words, additional functions may be useful, depending on the approach used) a. int searchByLength (char text [ ] [MAx LINE-LENGTH j, int n, int wordLen), that gets several lines of text, the number of lines of text and a length as input parameters and returns the number of occurrences of words with that length appearing in the text. b. int searchByWord (char text [ ] [MAX LINE LENGTH], ?nt n, char * keyword), that gets several lines of text, the number of lines of text and a keyword as input parameters and returns the number of occurrences of the keyword appearing in the text c. int searchByCharacter (char text[] [MAX LINE_LENGTH], int n, char ch), that gets several lines of text, the number of lines of text and a character as input parameters and returns the number of occurrences of the character (lowercase or uppercase) appearing in the text. d. void readText (char text [] [MAX LINE LENGTH], int n), that reads (gets) n lines of text from the user and stores them in the 2D text array; Assumptions 1- The maximum number of lines is 10. 2- Each text line might have different lengths (however, the maximum number of characters in any individual line is 80) 3- The maximum number of characters in any words in the text is 20 4- The list of delimiters to tokenize the lines (finding words) is ", . ;!'" Hint: You can use a two dimensional array or an array of pointers to save the text lines.

Explanation / Answer

Complete C Code with all the function implemented

Code is commented appropriately

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE_LENGTH 80

int searchByLength (char text[][MAX_LINE_LENGTH], int n, int wordLen);

int searchByWord (char text[][MAX_LINE_LENGTH], int n, char * keyword);

int searchByCharacter (char text[][MAX_LINE_LENGTH], int n, char c);

void readText(char text[][MAX_LINE_LENGTH], int n) {

FILE *file;

file = fopen("program.txt", "r"); /* open a text file for reading */

  

int nn=0;

while(fgets(text[nn++],80,file))

{

}   

  

//Debug by printing lines

// printf("%d ", n);

  

// for (i=0 ; i<n-1; i++)

// {

// printf("%s ", numbers[i]);

// }

fclose(file);

}

int main()

{

int i=0,j=0;

  

char numbers[20][MAX_LINE_LENGTH];

readText(numbers, 5);

char word[20] = "good"; //since max word is 20 given

int as = searchByWord(numbers, 5, word);

printf("%d ", as);

return 0;

}

int searchByLength (char text[][MAX_LINE_LENGTH], int n, int wordLen) {

int ans = 0; //Count of number of words with length equals to wordlen

int i = 0; //loop counter for number of lines

for(i = 0; i < n; i++) {

char * current_line = text[i]; //points to the current line

int j = 0; //to traverse whole line until null character

int count_word_length = 0;

while(current_line[j] != '') {

if(current_line[j] == ' ' || current_line[j] == ' ' || current_line[j] == ','|| current_line[j] == '.' || current_line[j] == ';' ) {

// printf("%d ", count_word_length++); //Debug length of each word in every string

if(count_word_length == wordLen) { //We have found a delimiter therefore test if condition is true

ans++;

}

count_word_length = 0; //resetting word_length to 0

j++;

continue;

}

count_word_length++; //increase by one since character is not a delimiter

j++; //next character

}

}

return ans;

}

int searchByWord (char text[][MAX_LINE_LENGTH], int n, char * keyword) {

int ans = 0; //Count of number of words equals to keyword

int i = 0; //loop counter for number of lines

for(i = 0; i < n; i++) {

char * current_line = text[i]; //points to the current line

int j = 0; //to traverse whole line until null character

int count_word_length = 0;

char current_word[20]; //Given max size of a word is 20

while(current_line[j] != '') {

if(current_line[j] == ' ' || current_line[j] == ' ' || current_line[j] == ','|| current_line[j] == '.' || current_line[j] == ';' ) {

current_word[count_word_length] = ''; //End the word by appending null character

// printf("%s ", current_word); //Debug print every word

//We have found a delimiter therefore test if current_word matches given keyword;

int ds = strcmp(current_word, keyword);

// printf("Q%dQ ", ds);

if(ds == 0) { //perfect match

ans++;

}

count_word_length = 0; //resetting word_length to 0

j++;

continue;

}

current_word[count_word_length] = current_line[j]; //append the character to current word

count_word_length++; //increase by one since character is not a delimiter

j++; //next character

}

}

return ans;

}

int searchByCharacter (char text[][MAX_LINE_LENGTH], int n, char c) {

int ans = 0; //Number of characters matched

int i = 0;

for(i = 0; i < n; i++) {

char * current_line = text[i]; //point to current line

int j = 0; //counter to traverse the current line

while(current_line[j] != '') { //loop until null character is found

//printf("%c ", current_line[j]); //Debug print each character

if(current_line[j] == c) {

ans++; //if match then increase

}

j++; //next character

}

}

return ans;

}