Move into the directory easy and retrieve the files located at In this task you
ID: 3761126 • Letter: M
Question
Move into the directory easy and retrieve the files located at
In this task you will implement the recursive function countWord. In the file count.c the signature for the function is located at the top for reference. This function takes in two items: a pointer to a Node and a string holding the word you are interested in. The function will recursively traverse the list and count how many times the word appears.
Here is an example of how your program should run with the given inputs
Another example given is
typedef struct node
{
char *value;
struct node *next;
} Node;
Node *
newNode(char *value,Node *next)
{
Node *n = malloc(sizeof(Node));
if (n == 0)
{
fprintf(stderr,"newNode: out of memory! ");
exit(1);
}
n->value = value;
n->next = next;
return n;
}
char *
head(Node *items)
{
return items->value;
}
Node *
tail(Node *items)
{
return items->next;
}
Node *
join(char *s,Node *rest)
{
return newNode(s,rest);
}
// Implement this method!
// Given a word we return how many times
// it exists in the list
int countWords(Node* n,char* word);
/* #START# */
/* Place implementation here */
/* #END# */
int main(int argc, char* argv[]) {
char buffer[512];
int len;
if(argc != 3) {
printf("usage: %s ",argv[0]);
return 0;
}
FILE* fp = fopen(argv[1],"r");
if(fp == NULL) {
printf("Could not open the file '%s' ",argv[1]);
return 0;
}
Node* head = NULL;
len = fscanf(fp," %s",buffer);
while(!feof(fp)) {
char* word = malloc(len + 1);
strcpy(word,buffer);
head = join(word,head);
len = fscanf(fp," %s",buffer);
}
fclose(fp);
len = countWords(head,argv[2]);
printf("The word "%s" appears %d time(s) ",argv[2],len);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct node
{
char *value;
struct node *next;
} Node;
Node *newNode(char *value,Node *next)
{
Node *n = malloc(sizeof(Node));
if (n == 0)
{
fprintf(stderr,"newNode: out of memory! ");
exit(1);
}
n->value = value;
n->next = next;
return n;
}
char *head(Node *items)
{
return items->value;
}
Node * tail(Node *items)
{
return items->next;
}
Node *join(char *s,Node *rest)
{
return newNode(s,rest);
}
// Implement this method!
// Given a word we return how many times
// it exists in the list
int countWords(Node* n,char* word)
{
if(n != NULL)
{
if(strcmp(n->value,word))
{
return countWords(n->next,word)+1;
}
else return countWords(n->next,word);
}
return 0;
}
int main(int argc, char* argv[])
{
char buffer[512];
int len;
if(argc != 3)
{
printf("usage: %s ",argv[0]);
return 0;
}
FILE* fp = fopen(argv[1],"r");
if(fp == NULL)
{
printf("Could not open the file '%s' ",argv[1]);
return 0;
}
Node* head = NULL;
len = fscanf(fp," %s",buffer);
while(!feof(fp))
{
char* word = malloc(len + 1);
strcpy(word,buffer);
head = join(word,head);
len = fscanf(fp," %s",buffer);
}
fclose(fp);
len = countWords(head,argv[2]);
printf("The word "%s" appears %d time(s) ",argv[2],len);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.