How to use struct for this purpose?
STEPS:
1. Declare an input string as follows:
char InputString[] = "the dog thought up a lie done";
Note that "done" is the last word that appears in the sentence, but not part of the sentence itself.
2. Use strtok() to extract each word from the InputString and store it in the WordList. (Assume space is the only delimiter separating the words).
Note: to access the "Word" array inside WordList use the "dot" operator. (eg. strcpy(WordList[n].Word, "hello"); )
Do not store the word "done" in the WordList, we are simply using it to mark the end of the sentence.
3. Print the contents of the WordList.
Here is what is done so far
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct WordStruct // Structure Definition
{
char Word[51]; // A "Word" is a string that can store up to 50 characters and the null string terminator
int length; // Number of characters stored in the Word (not including null)
} WordList[25]; // Declare an array to store 25 words
typedef struct WordStruct WS;
//void Printit(WS t);
int main()
{
char InputString[] = "the dog thought up a lie done";
int i;
WS s1;
s1 = InputString;
/*WordList[0] = strtok(InputString, " ");
for(i=1; i<9; i++)
{
WS[i] = strtok(NULL, " ");
}
*/
return 0;
}
clearly not doing it right. I thought I could get it into the array like this, in RED.
Someone please help. Structures is giving me a head ache.