/* File: pigs.c This file contains a program that translates from English to pig latin. It uses the strategy developed in class on Nov 7, 1995. The class had agreed on the following rules. Non-alphabetic characters such as digits and punctuation should be passed through. Build a string with the translated text. Extra white space should be removed. All upper case letters will be changed to lower case. The letter "y" is a vowel, always. The class had also decided to use a character by character strategy --- yuk :p This version does not deal with extra white space. */ #include #include "genlib.h" #include "simpio.h" #include "strlib.h" #include "ctype.h" /* Function Prototypes */ bool IsVowel(char) ; void HandleSpaces(char c) ; void HandlePrefix(char c) ; void HandleAlpha(char c) ; /* Type definition for the states of the program */ typedef enum {InBetween, Consonants, MiddleOfWord} state_type ; /* Global Variable */ state_type state ; string result ; string prefix ; main() { char c ; string input ; int index, length ; result = "" ; prefix = "" ; index = 0 ; state = InBetween ; printf("Enter an English sentence: ") ; input = GetLine() ; length = StringLength(input) ; /* Process the input line character by character */ for(index = 0 ; index <= length ; index++) { c = IthChar(input,index) ; c = tolower(c) ; switch(state) { case InBetween : HandleSpaces(c) ; break ; case Consonants : HandlePrefix(c) ; break ; case MiddleOfWord : HandleAlpha(c) ; break ; default : Error("Oops, something is very wrong!") ; } /* The debugging statements: printf("input[%d] = %c: %d\n result = %s\n prefix = %s\n", index, c, state, result, prefix) ; */ } printf("Pig Latin: %s\n", result) ; } /* This function handles the case where the program is scanning characters between the English words. */ void HandleSpaces(char c) { if (IsVowel(c)) { /* New English word, starting with a vowel */ prefix = "way" ; result = Concat(result, CharToString(c)) ; state = MiddleOfWord ; } else if (isalpha(c)) { /* New English word, starting with a consonant */ prefix = CharToString(c) ; state = Consonants ; } else { /* More between word characters */ result = Concat(result, CharToString(c)) ; } } /* This function handles the case where a word begins with a consonant. The initial sequence of consonants is stored in the global variable prefix. */ void HandlePrefix(char c) { if (IsVowel(c)) { /* End of consonants detected */ prefix = Concat(prefix, "ay") ; result = Concat(result, CharToString(c)) ; state = MiddleOfWord ; } else if (isalpha(c)) { /* More consonants detected */ prefix = Concat(prefix, CharToString(c)) ; } else { /* End of word detected. This word is all consonants. */ prefix = Concat(prefix, "ay") ; result = Concat(result, prefix) ; result = Concat(result, CharToString(c)) ; prefix = "" ; state = InBetween ; } } /* This function handles the case where the program is reading in the characters in the middle of an English word. */ void HandleAlpha(char c) { if (isalpha(c)) { /* Still in the word */ result = Concat(result, CharToString(c)) ; } else { /* End of the word is detected */ result = Concat(result, prefix) ; result = Concat(result, CharToString(c)) ; prefix = "" ; state = InBetween ; } } /* Returns true if the given character is a vowel. */ bool IsVowel(char c) { c = tolower(c) ; /* Check if c i s a vowel or a 'y' */ if ( (c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u') || (c == 'y') ) { return(TRUE) ; } else { return(FALSE) ; } }