Thread: pig latin help

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    pig latin help

    Hi,
    My assignment is on this website http://k4shif.netfirms.com/review.html
    I have written the code, exactly like the pseudo code provided, and is heavily commented with my thought process....but for some reason it does not do its job....i am posting the code, any help will be greatly appreciated coz this is due tomm...once again thanks
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    void translateline(char [], char []);
    int extractintoew(char [], char [],int);
    void convertintopig(char [], char [] );
    int wherevowel(char []);
    int copyintopl(char [], char [],int);
    
    int main()
    {
    	char el[100]={'\0'},pl[200]={'\0'};
    	int a=0;
    	
    
    	printf("Please enter a line(# to end): ");
    	gets(el);
    
    	while(el[0]!='#')
    	{
    		translateline(el,pl);
    		puts(pl);
    
    		
    
    		printf("\nPlease enter a line(# to end): ");
    		fflush(stdin);
    	    gets(el);
    	}
    
    	printf("Good Bye. Oodgay Ebyay\n");
    	system("PAUSE");
    	return 0;
    }
    
    void translateline(char el[], char pl[])
    {
    	char ew[17]={'\0'},pw[17]={'\0'};
    	int ep=0,pp=0,k=0,r;
    
    	while(el[ep]!='\0')
    	{
    		if(isalpha(el[ep]))
    		{
    			k=extractintoew(el,ew,ep);
    			ep+=k;//this is to keep everything at par, so even here, one behind space or punct
    			convertintopig(ew,pw);//copies the contents of pw into pl, pp at this point is still 0
    			r=copyintopl(pw,pl,pp);//this will return the value of pp, as to where it is in pl so that we can put the punct or space
    			pp+=r;//now pp is set at before that point
    			pp--;//now it is set for pp++
    			pp++;//now ready to accept punct or space position
    			ep++;//now at punct or space position
    		}
    		else
    		{
    			if(isspace(el[ep]))
    			{
    				el[ep]=pl[pp];
    			}
    			else
    			{
    				el[ep]=pl[pp];
    				strcat(pl," ");//after punctuation put a space
    			}
    		
    
    			
    		}
    		
    	}
    	pl[pp]='\0';//setting the last value to null as this is required by the output function
    	
    }//complete
    
    int extractintoew(char el[],char ew[],int ep)
    {
    	int i=0;
    
    	while(isalpha(el[i]))
    	{
    		ew[i]=el[ep];
    		i++;
    		ep++;
    	}
        return ep;//ep at this point is one place behind the space or punctuation and returns that number to 
    }//completed
    
    void convertintopig(char ew[], char pw[])
    {
    	int y,m;
    
    	y=wherevowel(ew);//incomplete at this point
    
    	if(y==0||y==(-1))//this indicates if it begins with a vowel, or has no vowel at all
    	{
    		strcat(ew,"way");
    		strcpy(pw,ew);
    	}
    	else
    	{
    		m=strlen(ew);//calculate the length of the string;
    		memmove(ew,&ew[y],m-y);// m-y indicates that number of alphabets to move
    		strcat(ew,"ay");
    		strcpy(pw,ew);//now pw, has the word with the vowels moved back, and truncated with "ay"
    	}
    }//completed
    
    int wherevowel(char ew[])
    {
    	int j;
    	char *vowel="aeiou";
    
    	if(ew[0]=='a'||ew[0]=='e'||ew[0]=='i'||ew[0]=='o'||ew[0]=='u')
    	{
    		j=0;
    		return j;
    	}
    
    	if(ew[0]=='A'||ew[0]=='E'||ew[0]=='I'||ew[0]=='O'||ew[0]=='U')
    	{
    		j=0;
    		return j;
    	}
    
    	else
    	{
    		j=strcspn(ew,vowel);
    
    		if(j==0)
    		{
    			j=(-1);
    			return j;
    		}
    		else return j;//this value here has the place where j occurs
    	}
    }//completed
    
    int copyintopl(char pw[], char pl[], int pp)
    {
    	int a=0;
    	int b;//declared for strlen of pl
    	b=strlen(pw);
    
    	while(a<b-1)
    	{
    		pw[a]=pl[pp];
    		a++;
    		pp++;
    	}
    
    	return pp;
    }//completed

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

    Re: pig latin help

    Originally posted by kashifk
    ...any help will be greatly appreciated coz this is due tomm...
    You're SOL...start earlier next time.

    Don't they not teach unit testing anymore?
    Code:
    void test()
    {
        //convertintopig() test vectors
        char t1[][17] = {"is","by","girl","Blue","The","black","cat","ate","the","mouse","by","a","pond"};
        char a1[][17] = {"isway","byway","irlgay","Ueblay","Ethay","ackblay","atcay","ateway","ethay","ousemay","byway","away","ondpay"};
    
        //translateline() test vectors
        char t2[][100] = {"The black cat ate the mouse by a pond.",
                          "My friend: John Smith ,the cat."};
        char a2[][100] = {"Ethay ackblay atcay ateway ethay ousemay byway away ondpay.",
                          "Myway iendfray: Ohnjay Ithsmay ,ethay atcay."};
    
        int size,n;
        
        //convertintopig() test
        size = sizeof(a1)/sizeof(a1[0]);
    
        for (n = 0; n < size; n++)
        {
            char t[17],a[17];
            strcpy(t,t1[n]); //convertintopig() will modify the source
    
            convertintopig(t,a);
    
            if (strcmp(a,a1[n]) != 0)
            {
                printf("convertintopig() is broke, %s -> %s, should be %s\n",t1[n],a,a1[n]);
            }
            else
            {
                printf("convertintopig() %s -> %s, passed\n",t1[n],a);
            }
        }
    
    
        //translateline() test
        size = sizeof(a2)/sizeof(a2[0]);
    
        for (n = 0; n < size; n++)
        {
            char pl[200]={0};
    
            translateline(t2[n],pl);
    
            if (strcmp(pl,a2[n]) != 0)
            {
                printf("translateline() is broke, \n\"%s\" \n->\n\"%s\"\nshould be\n\"%s\"\n",t2[n],pl,a2[n]);
            }
            else
            {
                printf("translateline() passed\n");
            }
        }
    }
    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    i know im so late....but i still wanna complete the program, so any other suggestions besides that???

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    That test function will expose all your bugs...get it so all the tests pass.

    I'll go ahead and give a few:
    - "pw[a]=pl[pp];" - wrong
    - "memmove(ew,&ew[y],m-y);" - wrong

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little help - Pig Latin function
    By bnmwad in forum C Programming
    Replies: 7
    Last Post: 04-25-2005, 04:31 AM
  2. pig latin help
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 04-01-2003, 10:10 PM
  3. Pig Latin problem
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2002, 01:44 PM
  4. Pig Latin
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-20-2002, 08:35 AM