Help with a method, Pointers, Cstrings, and Structs

This is a discussion on Help with a method, Pointers, Cstrings, and Structs within the C Programming forums, part of the General Programming Boards category; Okay so, I need to create a program that converts english phrases to pig latin. Moreover, I need to put ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Help with a method, Pointers, Cstrings, and Structs

    Okay so, I need to create a program that converts english phrases to pig latin. Moreover, I need to put them in a singly linked lists, specifically:
    I need to create two standard queues, implemented using singly linked lists, to hold my phrases. The head pointer to the first queue should be named EnglishPhrase, and it should point to the head of a singly linked list containing the original English phrase. The head pointer to the second queue should be named PigLatinPhrase, and it should point to the head of a singly linked list containing the translated Pig Latin phrase.

    There are two methods which must be incorporated into my program,
    WordNode* translateWord (WordNode* NextWord)
    Precondition: NextWord is passed in as a parameter and points to a WordNode containing and English word.
    Postcondition: A pointer to a new node containing the Pig Latin translation of NextWord is returned to the calling function using a return statement. NextWord remains unchanged.

    void printPhrase (WordNode* Phrase)
    Precondition: None.
    Postcondition: If Phrase points to a valid linked list, all the words contained in the list have been printed in the order that they appear in the list. If Phrase is a NULL pointer, an appropriate message is printed.

    For the time being, I would just like some guidance on how I should start the translateWord method. I understand how a linked list works, but I am not great at constructing them in C.

    Here is my Node:
    Code:
    #define MAX_WORD_CHARS (30)
    typedef struct WordNodeType
    {
       char word[MAX_WORD_CHARS];
       struct WordNodeType *next;
    }WordNode;
    WordNode *headptr = NULL;
    WordNode *tailptr = NULL;
    Thanks for any and all help.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    Your node type is okay, but if you can use a C99 compiler, I'd recommend
    Code:
    typedef struct WordNode  WordNode;
    struct WordNode {
       WordNode *next;
       char      word[];
    };
    so you would not have any kind of word length limitations. When allocating memory for this type of WordNode, you use the size of the structure plus the length of the string plus one for the string-terminating '\0'.

    Quote Originally Posted by Knight526806 View Post
    I would just like some guidance on how I should start the translateWord method.
    Your description states that it takes a WordNode containing an English word, and returns a dynamically allocated new WordNode containing the pig latin equivalent, without modifying the English word in any way.

    The rules are well described in the Pig Latin Wikipedia article. In short, you must allocate a string at least three characters longer than the original string (plus 1 for the end-of-string '\0', plus 1 if you add a dash), then use the applicable rules to copy and mangle the original string into the new one. Examples:

    1. (b)(ook) ⇒ (ook)-(bay)
    2. (sw)(eater) ⇒ (eater)-(sway)
    3. (qu)(estion) ⇒ (estion)-(quay) or (q)(uestion) ⇒ (uestion)-(qay)
    4. (answer) ⇒ (answer)-(way) or (answer) ⇒ (answer)-(ay)
    5. (error) ⇒ (error)-(way) or (error) ⇒ (error)-(ay)


    The way I would approach this is to write a helper function, that returns the length of the initial prefix that will be moved to the end of the result (and ay appended to it). If it returns zero, then the word starts with a vowel sound, and you just append -way or -ay.

    The helper function should recognize consonants or the cluster of letters that form the initial consonant sound. To make the tests easier, you could use a short array (say five characters) for the initial word, where you copy the letters in lower case (and if shorter, fill the array with '\0'). Then, maybe a switch statement around the initial (lowercase) letter, with followup if tests within the respective cases?

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Okay so I am doing this set by step, right now I am trying to make a translate method but again, I am not very good with pointers. I did however successfully convert an english word to piglatin under the piglatin guide which I am supposed to follow:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
     
     
    int main()
    {
                    char word[30],word1[30], word2[30], ch;
                    int i,j=0;
     
                    printf ("\n\n  enter word: ");
                    gets (word);
     
                    ch=word[0];
                    word1[0] = ch;
     
                    int length = strlen(word);
        for (i=1;i<length;i++)
                    {
            word2[j] = word[i];
            j++;
                    }
                    word2[j] = tolower(word1[0]);
                    char x = toupper(word2[0]);
                    word2[0]= x;
        word2[j+1] = '\0';
     
        printf  ("\n  now it look like this:%s",word2);
     
        if((length > 1) && (ch == 'a' || ch == 'A'||ch =='e'||ch =='E'||ch =='i'||ch == 'I'||ch =='o'||ch =='O'||ch =='u'||ch =='U'))
        {
            strcat(word2, "tay");
        }
        else
        {
            strcat(word2, "ay");
        }
         printf  ("\n  now it look like this:%s",word2);
         return 0;
    }
    Now I am trying to convert this into a method: char* translate(char* Englishword)
    any help on getting the pointers correct for this?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    Ah, your pig-latin rules are much simpler: You always move the first character to the end of the string. If it was A a E e I i O o U u, you append "tay", otherwise you append "ay".

    The pseudocode you need is actually very simple:
    1. Find the length of the original string.
    2. Allocate enough room for the result.
      length + 3 (for ay or tay) + 1 (for '\0') is always enough.
    3. Copy the original string starting from the second character to the dynamically allocated storage, using strcpy() or memcpy().
      Note that the pointer to the second character is (EnglishWord + 1), and the length sans the first character is obviously length-1.
    4. Append the initial character from the original string, possibly converting it to lower case.
      Note that the index of this character in the dynamically allocated storage is length-1.
      (If you use an str..() function for the next step, remember to add the end-of-string mark too, at index length .)
    5. Append the proper suffix, either "ay" or "tay" , using strcat(), strcpy(), or memcpy().
    6. Unless you took care of it already in the previous step, make sure you have an end-of-string '\0' at the end. Done.

    Note: You do not need any loops, the above is sufficient.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Alright I did it,
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    char word[30],word1[30], word2[30], ch;
    char* translate(char Englishword[])
    {
     int i,j=0;
     strcpy(word, Englishword);
     ch=word[0];
     word1[0] = ch;
     int length = strlen(word);
        for (i=1;i<length;i++)
     {
            word2[j] = word[i];
            j++;
     }
     word2[j] = tolower(word1[0]);
     char x = toupper(word2[0]);
     word2[0]= x;
        word2[j+1] = '\0';
        printf  ("\n  now it look like this:%s",word2);
        if((length > 1) && (ch == 'a' || ch == 'A'||ch =='e'||ch =='E'||ch =='i'||ch == 'I'||ch =='o'||ch =='O'||ch =='u'||ch =='U'))
        {
            strcat(word2, "tay");
        }
        else
        {
            strcat(word2, "ay");
        }
         printf  ("\n  now it looks like this:%s",word2);
         return word2;
    }
    int main()
    {
        char input[30];
     printf ("\n\n  enter word: ");
     gets (input);
    translate(input);
         return 0;
    }
    Now I need to do the linked list part and create translateWord method described above.
    Any guidance on the pointers?

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    Quote Originally Posted by Knight526806 View Post
    Code:
    char* translate(char Englishword[])
    {
     int i,j=0;
     strcpy(word, Englishword);
    Like I said, you can copy the English word from the second character onwards. That way you do not need any loops in your code.

    Instead, above, you copy the original word, then do some kind of loop to move the letters. Why? Why not follow the procedure I outlined in post #4? Did you just copy the function from someone else, and are trying to get me to do your homework for you?

    Also, you are using a global variable to hold the result. Why don't you allocate it dynamically? You'll need to do dynamic allocation anyway for your linked list; if you try the same global variable approach with linked lists, it'll fail miserably.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 08:43 PM
  2. help with structs and pointers in C++
    By casper29 in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2011, 09:51 AM
  3. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  4. Method pointers and inheritance
    By Magos in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2006, 06:43 PM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21