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

Write a C program that does the following, please follow the detailed instructio

ID: 3702628 • Letter: W

Question

Write a C program that does the following, please follow the detailed instructions throughly, it is only one question, and the instructions make it very clear for you :

Overview This assignment covers strings, structures (potentially, depending on your solution method), file I/O and problem solving techniques. The goal of the assignment is to write C programs which can parse and analyze English text, and the three parts of the assignment represent different milestones in producing a program to analyze the word distribution in a passage of text. Definition of a word Since this assignment requires reading words from a passage of text, it is important to have a rigorous defintion of what constitutes a word. For this assignment, a word is more of the following any sequence of 1 or » Alphabetical characters (either uppercase or lowercase) The hyphen character - The apostrophe character ' For extra clarity, the function is word_character below returns 1 if the provided character meet:s the definition of a word character above and returns 0 otherwise. You don't have to use this function in your code; it is only provided as a source of context. int is word_character(char c)f if (isalpha(c) return 1; c'-' llc- ' else return 0; Words in a passage of tex Your code will need to extract words from a consecutive set of word characters (as defined above) as a word and use any non-word characters (like spaces, newlines, digits or symbols other than hyphens or apostrophes) as a separator of text. To do this, you should take any For example, the string "Raspberry Pineapple" contains two words: "Raspberry" and "Pineapple". The string "Rasp###berry Pine123apple and "apple". Notice that any non-word character can be used to break up words, and that repeated sequences of non-word characters are all ignored while finding words. "contains four words: "Rasp", "berry", "Pine" For each of the three parts of this assignment, you will write a program which reads a text file input text.txt and extracts all of the words from the file. If you complete the parts in order, you should find that the solution for each part helps you complete the next part. Since our definition of a word is slightly different than usual (in particular, words do not include characters like digits and certain symbols), you cannot directly read words from the input file using fscanf. You will likely find that the easiest way to read words is to read the file character by character with fgetc and build word works. s as you go, but you are free to use any input method that

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define M 10000

/*
size_t hash(char *word){
size_t counter, hashAddress =0;
for (counter =0; word[counter]!=''; counter++){
hashAddress = hashAddress*word[counter] + word[counter] + counter;
}
return (hashAddress%M);
}*/

int is_word_char(char c){
return isalpha(c) || c=='-' || c==''';
}

void print_words(char *words[],size_t n){
int i;
for(i=0;i<n;i++){
printf("%s ",words[i]);
}
}

int wordIn(char *w,char *wlist[],size_t len){
//same as uwords[hv=hash(word)]==NULL
size_t i;
for(i=0;i<len;i++){//compare against the list of words
if(strcmp(w,wlist[i])==0){
return 1;//true
}
}
return 0;//false
}

int main(){
FILE* fp=fopen("./input_text.txt","r");
if(fp == NULL){
printf("FILE could not be opened ");
exit(1);
}
char *s;
char word[101];
size_t buf=100;
char *swords[20] = {0};
char *lwords[20] = {0};
char *uwords[M] = {0};
int hv;
size_t i,j,len,wl;
size_t sn=0,ln=0,sl=101,ll=0;
size_t un=0,n=0;
while((len = getline(&s,&buf,fp)) != -1){
for(i=0,j=0;i<len;i=j){
while(j<len && is_word_char(s[j++]));
if(j-i-1==0)continue;
strncpy(word,s+i,j-i-1);
word[j-i-1]='';
n++;
if(!wordIn(word,uwords,un)){
uwords[un] = strdup(word);
if((wl = strlen(uwords[un]))<sl){
swords[0] = uwords[un];
sn = 1;
sl = wl;
}else if(wl==sl){
swords[sn++] = uwords[un];
}
if(wl>ll){
lwords[0] = uwords[un];
ln = 1;
ll = wl;
}else if(wl==ll){
lwords[sn++] = uwords[un];
}
un++;
}
}
}
fclose(fp);
printf("Total words= %lu ",n);
printf("Unique words= %lu ",un);
printf("Shortest word(s): ");
print_words(swords,sn);
printf("Longest word(s): ");
print_words(lwords,ln);


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote