/* * The Actual Pig Latin Word Making function * * This function takes a word (all letters terminated * with a NULL) and converts it into Pig Latin following * some made-up rules. The word is stored in the * piglatin[] buffer. Various string routines copy and * concatenate parts of the re-constructed word into * that buffer. The original starting letter is also * saved in a buffer, append[], since only strings can * be concatenated to each other, not single chars. * * A pointer variable saves the original address of the * word being translated, so the word is send and * returned as the same variable. This saves going * through the pains of returning a string from a * function. */ #include #include #include void iglatinpay(char *english) { char piglatin[32]; //temporary word sto. char *e; char append[] = "h"; //first letter sto. char ch; /* * Start by saving the starting address of the word * being sent to this function. The E pointer variable * will remember that for you. */ e = english; /* * All Pig Latin is written lower case with initial * caps. (At least that's the easy way to handle * things here.) */ strlwr(english); //make it all lower case /* * RULES FOR TRANSLATING ENGLISH INTO PIG LATIN * As told to a switch-case loop * * First rule: Words starting with a vowel * are merely given the AY ending. * * Note how strcpy() is used first, then strcat() * is used to continue building the Pig Latin word * * Also: See how the case statements "fall through," * allowing several of them to catch common * situations. */ ch = *english; //save the first character switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': strcpy(piglatin,english); strcat(piglatin,"ay"); break; /* * Second rule: Words starting with SH, CH * TH, PH, RH, WH, QU have both letters moved * to the end before adding AY */ //continuing switch-case case 'c': case 'p': case 'r': case 's': case 't': case 'w': if(english[1]=='h') { english+=2; //point at 3rd char // after ?H sound strcpy(piglatin,english); //add on the starting letter append[0] = ch; strcat(piglatin,append); //and then the 'H' and "AY" strcat(piglatin,"hay"); break; } case 'q': if(english[1]=='u') { english+=2; strcpy(piglatin,english); append[0] = ch; strcat(piglatin,append); strcat(piglatin,"uay"); break; } /* * Standard rule: Move the first letter to the * end of the word and add AY */ //continuing switch-case default: english++; strcpy(piglatin,english); append[0] = ch; strcat(piglatin,append); strcat(piglatin,"ay"); break; } /* * To "return" the word to the calling function * you'll simply copy the word you constructed (which * is in the piglatin[] buffer) to the location of the * word being translated, which was saved in the E * pointer. */ strcpy(e,piglatin); /* * And finally, make the initial cap of each word */ *e = toupper(*e); }