In: Computer Science
Nearby is a main() function demonstrating the use of the function earliest_word. Implement this function according to the documentation given. My solution is about 25 lines plus some closing curly braces.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *earliest_word(char *fname, int *nwords);
// Opens the file fname and reads words from it
// until the end of file is reached then closes
// the file. If the file to be opened doesn't
// exist, returns NULL and sets nwords to
// -1. Tracks the alphabetic "earliest" word that
// is read in as indicated by strcmp(). Tracks how
// many words in total are read and sets nwords to
// that value. Allocates a block of memory and
// copies the earliest word found into the block
// using strcpy(). Returns a pointer to the
// freshly allocated block.
//
// ASSUMPTIONS: Words are no longer than 127
// characters so will fit in an array of size
// 128. Files have at least one word in them.
int main(){
int count; char *file; char *early;
file = "vegetables.txt";
// pumpkin carrot beet squash cucumber
early = earliest_word(file, &count);
printf("%s: %d words, %s earliest\n",
file,count,early);
// vegetables.txt: 5 words, beet earliest
free(early);
file = "fruits.txt";
// banana peach orange apple pineapple strawberry
early = earliest_word(file, &count);
printf("%s: %d words, %s earliest\n",
file,count,early);
// fruits.txt: 6 words, apple earliest
free(early);
file = "not-there.txt";
early = earliest_word(file, &count);
if(early==NULL){
printf("%s not found\n",file);
// not-there.txt not found
}
return 0;
}
C programming
#include <stdio.h>
#include <stdlib.h>
/**
* Global variables
*/
FILE *fileToRead;
char ch;
int num_characters, nwords, num_lines;
void earliest_word(char *fname, int nwords) {
fileToRead = fopen(fname, "r");
/* Check if the file is opened*/
if (fileToRead == NULL) {
printf("Problem occurred will trying to open the file.\nCheck if the file exists or provide the right path\n");
// set file nwords to negative one
nwords = -1;
exit(EXIT_FAILURE);
}
/*
* Logic to count num_characters, nwords and num_lines.
*/
num_characters = nwords = num_lines = 0;
while ((ch = fgetc(fileToRead)) != EOF) {
num_characters++;
/* new line checker*/
if (ch == '\n' || ch == '\0')
num_lines++;
/* Words checker */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
nwords++;
}
/* Increment nwords and num_lines for last word */
if (num_characters > 0) {
nwords++;
num_lines++;
}
/* Print fileToRead statistics */
printf("\nTotal num_lines in the file = %d\n", num_lines);
printf("Total number of words in the file = %d\n", nwords);
printf("Total number of characters in in the file = %d\n", num_characters);
/* Closing the file after read completion*/
fclose(fileToRead);
}
// main function
void main() {
// file to read pointer declaration
FILE *fileToRead;
//character array of file filePath to read
char * filePath[100];
//file entry
printf("Please provide the source filePath: ");
scanf("%s", filePath);
/* Open the fileToRead in read mode */
fileToRead = fopen(filePath, "r");
//operation process
//External method call
earliest_word(filePath,nwords);
}
OutPut:

_________________________________
Comment Down For Any Queries.
Please Give a Thumbs Up If You are satisfied With The Answer.