#include #include #include #include int isVowel(char c) { /* BEGIN ANSWER (a) */ switch ( toupper(c) ) { case 'A': case 'E': case 'I': case 'O': case 'U': case 'Y': return 1; default: return 0; } /* END ANSWER (a) */ } void rotateWord(char w[], int n) { /* BEGIN ANSWER (b) */ char tmp; int i; tmp = w[0]; for (i=1; i < n; i++) { w[i-1] = w[i]; } w[n-1] = tmp; /* END ANSWER (b) */ } void printPig(char *w) { /* BEGIN ANSWER (c) */ if ( isVowel(w[0]) ) { printf("%sway",w); } else { int n = strlen(w); int count = 0; /* We should check for complete rotation, in case of "words" without a vowel. But they aren't required to do this. Anybody who does notice and checks for a loop here gets a bonus */ while ( ! isVowel(w[0]) && count++ < n ) rotateWord(w,n); printf("%say",w); } /* END ANSWER (c) */ } const int LENGTH = 256; int main() { int c; char word[LENGTH]; /* should be long enough for any word in English! */ int wlen = 0; /* length of word that we've read */ /* read words and non-words, print pigLatin of words and print non-words as is */ while ( (c = getchar()) != EOF ) { /* is c a letter? */ if ( isalpha(c) ) { /* add it to the word */ word[wlen++] = c; if ( wlen >= LENGTH ) { fprintf(stderr,"Reading a word too long to store!\n"); return EXIT_FAILURE; } } else { /* not a letter. If we have a word, print it */ if ( wlen > 0 ) { /* terminate the word */ word[wlen] = '\0'; printPig(word); /* clear the word */ wlen = 0; } /* and print the non-word character */ putchar(c); } } /* print any remaining word */ if ( wlen > 0 ) { word[wlen] = '\0'; printPig(word); } return EXIT_SUCCESS; }