Thread: A little help - Pig Latin function

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    A little help - Pig Latin function

    Slow but sure and I am finally close. Below is a function I am calling from a menu. It is to convert a sentence for English to Pig Latin. I have not found where I went wrong yet (any help would be appreciated). To let you know where I am at, here is the a sample input and a sampl of whay I get returned right now:
    input- bring me the butter
    out put- ingbray ebay ebay utterbay

    Code:
    int pig()
    {
    char sent[150];
    char part[40];
    char temp[40];
    char *tokenPtr;
    char vowel []= "aeiou";
    char *ptrloc;
    int llen=0;
    int tlen=0;
    int k=0; 
     
    printf("Please enter a sentence\n");
    
    gets(sent);   
    
    tokenPtr=strtok(sent, " ");
    
    while (tokenPtr !=NULL){
    
    sscanf(tokenPtr, "%s", part);
     
    ptrloc=strpbrk(part, vowel);
    
    tlen=strlen(part);
    
    llen=strlen(ptrloc);
    
    for (k; k < (tlen-llen); k++){
    
    temp[k]=part[k];
    
    }
    temp [ (tlen-llen)]='\0';   
    
    printf(" %s", ptrloc);
    
    printf("%s", temp);
    
    tokenPtr=strtok(NULL, " ");
    }
    printf("\n\n"); 
     
    return 0;
    }

    Thanks for any help or advice!

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    That code is a bloody mess. Use some tabs to space everything and include functions so we can see the rest of the code.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Sorry about that --not sure what I managed to do to lose my formatting. Here is the complete program I am working on. As mentioned in the previous post, the pig() function is my trouble spot. Here is the a sample input and a sample of what I get returned right now:

    input- bring me the butter
    out put- ingbray ebay ebay utterbay

    Code:
    # include <stdio.h>
    # include <string.h>
    
    int name();
    int pig();
    int izzle();
    
    main()
    {
     int y;
    
       while ( y != 4) 
       {
    
        printf("Welcome to the word game!\n");
        printf("Please select from the following list:\n");
        printf("1. Name Game\n 2. Pig Latin\n 3. My Game\n 4. Exit\n");
        scanf("%d", & y);
    	fflush(stdin);
     
    	   if (y==1) 
    		  name();
           else
           if (y==2)
              pig();
           else
           if (y==3)
              izzle();
       }
    }
    
    
    int name()
    {
      char word[40];
      char bword[40];
      char fword[40];
      char mword[40];
      char oword[40];
    
      int h=1;
      int l=1;
      int w=1;  
     
      printf("Enter your name\n");
    
      scanf("%s", word);
      sscanf(word, "%s", oword);
    
      mword[0]='M';
      fword[0]= 'F'; 
      bword[0]= 'B';
    
      printf("\n\n%s!\n%s, %s bo", word,word,word);
    
         for (h=1; (bword[h]=word[h]) !='\0'; h++){
    		 printf("%s Bonana fanna fo", bword);}
     
         for (l=1; (fword[l]=bword[l]) !='\0'; l++) {
             printf("%s\nFee fy mo", fword);}
    
    	 for (w=1; (mword[w]=fword[w]) !='\0'; w++) {
             printf("%s, %s!\n\n", mword, oword); }
    }
    
    
    
    int pig()
    {
      char sent[150];
      char part[40];
      char temp[40];
      char *tokenPtr;
      char vowel []= "aeiou";
      char *ptrloc;
      int llen=0;
      int tlen=0;
      int k=0; 
     
      printf("Please enter a sentence\n");
      gets(sent);   
    
      tokenPtr=strtok(sent, " ");
    
         while (tokenPtr !=NULL)
    	 {
            sscanf(tokenPtr, "%s", part); 
            ptrloc=strpbrk(part, vowel);
            tlen=strlen(part);
            llen=strlen(ptrloc);
        
               for (k; k < (tlen-llen); k++)
    		   {
                  temp[k]=part[k];
    		   }
       temp [ (tlen-llen)]='\0';
       printf(" %s", ptrloc);
       printf("%say", temp); 
    
       tokenPtr=strtok(NULL, " ");
    	 }
      
       printf("\n\n"); 
       return 0;
    }
    
    
    
    int izzle()
    {
       char dumb[150];
       char *tokenPtr;
       printf("Please enter a sentence:\n");
       gets(dumb);
       tokenPtr = strtok(dumb, " ");
    
          while(tokenPtr != NULL)
    	  {
             printf(" %sizzle ", tokenPtr);
             tokenPtr=strtok(NULL, " ");
    	  }
    
       printf("\n");
       return 0;
    }
    Again any help is appreciated !

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    made for cpp but should be easy to change for c

    Code:
    /*====================================
      Bring me the butter
      
      = ringbay emay hetay utterbay!
      
      
    
    Algo
    1. Grab first letter move to the end 
    then print 'ay'. What could be simpler.
    
    ======================================*/
    
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        
        char array[81];
        cin.getline(array,81);
        
        fstream file_pointer;
        file_pointer.open("pig.txt",ios::out);
        
        do{
            
            file_pointer<<array;
            
          }while(file_pointer.peek()!=EOF);
          
           file_pointer.close();
    
         fstream read;
         read.open("pig.txt",ios::in);
         char array_two[81];
         do{
             read>>array_two;
             int size=strlen(array_two);
             
             for(int a=0; a<size; a++)
             {
                 if(a!=0)
                 {
                     cout<<array_two[a];
                 }
             }
             cout<<array_two[0]<<"ay ";
         }while(read.peek()!=EOF);
         read.close();
         
         int pause;
         cin>>pause;
     }

  5. #5
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    treenef: your results don't match the required results

    bnmwad: You had it, all but for the sake of two characters... check out the changes

    Code:
    # include <stdio.h>
    # include <string.h>
    
    int name();
    int pig();
    int izzle();
    
    main()
    {
     int y;
    
       while ( y != 4) 
       {
    
        printf("Welcome to the word game!\n");
        printf("Please select from the following list:\n");
        printf("1. Name Game\n 2. Pig Latin\n 3. My Game\n 4. Exit\n");
        scanf("%d", & y);
    	fflush(stdin);
     
    	   if (y==1) 
    		  name();
           else
           if (y==2)
              pig();
           else
           if (y==3)
              izzle();
       }
    }
    
    
    int name()
    {
      char word[40];
      char bword[40];
      char fword[40];
      char mword[40];
      char oword[40];
    
      int h=1;
      int l=1;
      int w=1;  
     
      printf("Enter your name\n");
    
      scanf("%s", word);
      sscanf(word, "%s", oword);
    
      mword[0]='M';
      fword[0]= 'F'; 
      bword[0]= 'B';
    
      printf("\n\n%s!\n%s, %s bo", word,word,word);
    
         for (h=1; (bword[h]=word[h]) !='\0'; h++){
    		 printf("%s Bonana fanna fo", bword);}
     
         for (l=1; (fword[l]=bword[l]) !='\0'; l++) {
             printf("%s\nFee fy mo", fword);}
    
    	 for (w=1; (mword[w]=fword[w]) !='\0'; w++) {
             printf("%s, %s!\n\n", mword, oword); }
    }
    
    
    
    int pig()
    {
      char sent[150];
      char part[40];
      char temp[40];
      char *tokenPtr;
      char vowel []= "aeiou";
      char *ptrloc;
      int llen=0;
      int tlen=0;
      int k=0; 
     
      printf("Please enter a sentence\n");
      gets(sent);   
    
      tokenPtr=strtok(sent, " ");
    
         while (tokenPtr !=NULL)
    	 {
            sscanf(tokenPtr, "%s", part); 
            ptrloc=strpbrk(part, vowel);
            tlen=strlen(part);
            llen=strlen(ptrloc);
        
               for (k=0; k < (tlen-llen); k++)
    		   {
                  temp[k]=part[k];
    		   }
       temp [ (tlen-llen)]='\0';
       printf(" %s", ptrloc);
       printf("%say", temp); 
    
       tokenPtr=strtok(NULL, " ");
    	 }
      
       printf("\n\n"); 
       return 0;
    }
    
    
    
    int izzle()
    {
       char dumb[150];
       char *tokenPtr;
       printf("Please enter a sentence:\n");
       gets(dumb);
       tokenPtr = strtok(dumb, " ");
    
          while(tokenPtr != NULL)
    	  {
             printf(" %sizzle ", tokenPtr);
             tokenPtr=strtok(NULL, " ");
    	  }
    
       printf("\n");
       return 0;
    }
    BTW - this will compile, but not without warnings

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Lightbulb Thanks

    Thanks for the help !! I figured as usual it was something simple I just kept missing!!

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    treenef: your results don't match the required results

    Remember pig latin has various dialects, which means there is no correct/ incorrect output.

    http://www.snowcrest.net/donnelly/piglatin.html

    For example, in most pig latin conversions the rule is, if the word begins with a vowel, output the whole word and then add 'way' onto the end.

    Code:
    is  =   isway

    Since this is a very trivial exception to my program it would be very easy to correct. Moreover, a few other cases exist where pig latin differs from place to place.

    Of course, my output may not be the same as the one expected in the original post, however, I was just demonstrating another of tackling the problem using fstreams.

    It wasn't in my interests to write a complete program. I have left that to the author of the original post

  8. #8
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Tis ok, I was just being picky

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM