Thread: How to use struct for this purpose?

  1. #1
    Registered User
    Join Date
    Apr 2008

    How to use struct for this purpose?

    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
    #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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Why would you think that? strtok returns a character pointer. WS[i] isn't a character pointer. Also, strtok doesn't actually duplicate or copy anything. It uses a local static buffer to contain a portion of its arguments. You still have to end up copying some place, and you have to do that yourself. Furthermore, arrays start at 0 not 1.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Structures shouldn't give you a headache.... structures are very simple things. I think you have a headache because you think the struct will do a lot more than it does.

    I'm not sure I understand what you're trying to do though, so apologies if I missed the point.

    You have
    Wordstruct: a struct which contains a char array and an int
    WordList: An array of 25 these structs

    So to copy the words from InputString to Wordlist you'll have to strcpy one element at a time.

      strcpy(WordList[0].Word, strtok(InputString, " "));
      WordList[0].length = strlen(WordList[0].Word);
      for(i=1; i<9; i++) 
          strcpy(WordList[i].Word, strtok(NULL, " "));
          WordList[i].length = strlen(WordList[i].Word);
    You can't take a char array and assign it to an array of a different type. Nor can you do
    struct WordStruct myws = "some string";
    The compiler doesn't know you mean "copy the string into the char array and store the length in the int". How could it?

  4. #4
    Registered User
    Join Date
    Apr 2008


    I see where I went wrong.

    Thank you for showing me this. I appreciate this more than you will know. I believe I understand now how you went about this.

    I followed that with this.

    for(i=0; i<10; i++)
          printf("%s\n", WordList[i].Word); 
    Once again, thank you so much for taking the time for this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM