Question
C programming If someone could help me with this question I'd really appreciate it Modify this assignment.c program per fthe following instructions: 1- to read
C programming
If someone could help me with this question I'd really appreciate it
Modify this assignment.c program per fthe following instructions:
1- to read the name of the input file from a command line argument. The input file to read is: words.txt
2- replace any array declaration with the dynamic memory allocation using malloc() C library function.
assignment.c
#include
int main() {
char words[10][20];
FILE* ifp;
// Open the input file (required to have 10 words).
ifp = fopen("words.txt", "r");
// Read in the words into the array words.
int i;
for (i=0; i<10; i++)
fscanf(ifp, "%s", words[i]);
// Get the word to search for.
char searchword[20];
printf("Enter a word for which to search? ");
scanf("%s", searchword);
// Print out an appropriate message, based on the search results.
if (searchForWord(words, 10, searchword))
printf("Your word was in the list! ");
else
printf("Sorry, your word was NOT on the list. ");
system("PAUSE");
return 0;
}
// Returns 1 iff word is in list. list must have length strings in it.
int searchForWord(char list[][20], int length, char word[]) {
int i;
// Go through each word.
for (i=0; i // See if it's a match. if (strcmp(list[i], word) == 0) return 1; } // If we get here, no match was found. return 0; } words.txt fish dog cat elephant aardvark squirrel mouse llama duck bird
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