Thread: rate my code

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

    rate my code

    Hi
    I wrote this program that converts a sentence into pig latin. I wrote this a while ago, now just wanna see if i can improve it and see what other people think about it
    It follows these rules:-
    If the word begins with a vowel -way is appended at the end.
    An become Anway
    If the word has no vowel -way is appended at the end.
    By becomes Byway
    Otherwise all the letters following the first vowel in the word are moved to the front and -ay is added to the end.
    Pond becomes Ondpay.

    here is the code, tell me what ya think and how i can improve it:-

    Code:
    /* This program takes in a line of text, and converts into pig latin, and the rules*/
    /*of pig latin are explined in the program details*/
    /*programmer = Kashif Khan. 139-06-1856*/
    /*TTH 4:00-5:30*/
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    
    /*function prototypes*/
    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'};
    	
    	
    
    	printf("Enter a line(# to end): ");
    	gets(el);
    
    	while(el[0]!='#')
    	{
    		translateline(el,pl);
    		printf("Pig Latin translation: ");
    		puts(pl);
    
    		
    
    		printf("\nPlease enter a line (# to end): ");
    		fflush(stdin);
    	    gets(el);
    	}
    
    	printf("Good Bye, Oodgay Ebyay\n");
    	system("PAUSE");
    	return 0;
    }
    
    /*based on pseudo code shown in instruction*/
    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;
                convertintopig(ew,pw);
                r = copyintopl(pw,pl,pp);
                pp += r;
            }
            else
            {
                if (isspace(el[ep]))
                    pl[pp++] = el[ep];
                else
                {
                    pl[pp] = el[ep];
                    pp++;
                }
    
                ep++;
            }
            
        }
    
        pl[pp] = '\0';
        
    }
    
    /*extracts word from el that starts from current position*/
    /*and store in ew*/
    int extractintoew(char el[],char ew[],int ep)
    {
        int i = 0;
    
        while (isalpha(el[ep]))
            ew[i++] = el[ep++];
    
        ew[i] = '\0';
    
        return i;
    }
    
    /*translate English word ew to Pig Latin and store in pw*/
    void convertintopig(char ew[], char pw[])
    {
        int y, m, cap = 0;
    
        /* Check for a capital letter */
        if (ew[0] == toupper(ew[0]))
        {
            ew[0] = (char)tolower(ew[0]);
            cap = 1;
        }
    
        y = wherevowel(ew);
        if (y == 0 || y == -1)
        {
            strcat(ew,"way");
            strcpy(pw,ew);
        }
        else
        {
            m=strlen(ew);//calculate the length of the string;
            memmove(pw, ew + y, m); /* Copy the latter half to pw */
            strncat(pw, ew, y);     /* Copy the former half to pw */
            strcat(pw, "ay");       /* End the word */
        }
    
        /* Deal with the capital letter */
        if (cap == 1)
            pw[0] = (char)toupper(pw[0]);
    }
    
    /*determines the position of the first vowel*/
    
    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'||ew[0]=='A'||ew[0]=='E'||ew[0]=='I'
            ||ew[0]=='O'||ew[0]=='U')
        {
            j = 0;
        }
        else
        {
            char *p = strpbrk(ew,vowel);
            j = (p == 0) ? -1 : p - ew;
        }
    
        return j;
    }
    
    /*copies word from pw into pl*/
    int copyintopl(char pw[], char pl[], int pp)
    {
        int a = 0;
        int b;
    
        b = strlen(pw);
    
        while (a < b)
            pl[pp++] = pw[a++];
    
        return b;
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    gets() was used because those were the professor instructions. And the arrays were such bec the english word or sentence would be smaller than the pig latin equivalent. I used memmove to be different from the rest of the ppl, bec most would use a loop or strcpy, i wanted to be a little diff. As for the other suggestion, they are excellent and I will implement them...please keep the suggestions pouring in and what you think of the program overall.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM