C Programming Create a function that will return the nth wor
C Programming:
Create a function that will return the nth word in a file. It should accept a file
pointer, an int indicating the position, and a char array. Should look something like
this:
int getWordAt(FILE *fp, int pos, char *word)
int pos specifies the nth word you’re looking for. For example, if I want to know what the
200th word is in the file, I would set pos=200. char * word is a char array that
will hold the actual word that is found at that position (if it is found). So, before I
call getWordAt() from main(), I need to define an array to hold the value and
then pass that into the getWordAt(). If I were to call the function from main, it
might look something like this:
char word[MAX_WORD_LEN];
// get the 200th word from file
int wordPos = getWordAt(fp,200, word);
if (wordPos!=)
// indicate we found the word, and then print the word to console
else
// indicate that we could not get any word at that location
The function will loop until it hits the position where the word is or until EOF. If the
function finds the word, it will return the same value as pos, signifying that it did
find the word; returns 0 otherwise.
Solution
#include