/* File: piggy.c

   This file contains a program that translates
   from English to pig latin.  It uses the 
   strategy developed in class on Nov 15, 1994.
   The class had agreed on the following rules.

       No punctuation.
       Build a string with the translated text.
       Non-alphabetic characters should be passed through.
       Extra white space should be removed.
       All upper case letters will be changed to lower case.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

/* Function Prototypes */
int FindWord(string, int) ;
int WordEnd(string, int) ;
bool IsVowel(char) ;
string Translate(string) ;
int FirstVowel(string) ;



main() {
   string input, word, result ;
   int start_pos, end_pos, index ;

   result = "" ;
   index = 0 ;

   printf("Enter an English sentence: ") ;
   input = GetLine() ;

   /* Process the input line word by word */

   while (TRUE) {

      /* Find beginning of the word */
      start_pos = FindWord(input, index) ;

      if (start_pos < 0) break ;

      /* Find the end of the word */
      end_pos = WordEnd(input, start_pos) ;

      word = SubString(input, start_pos, end_pos) ;
      index = end_pos + 1 ;

      /* If real word translate to Pig Latin */
      if ( isalpha(IthChar(word,0)) ) {
	 word = Translate(word) ;
      }

      /* Store word in result */
      if (StringEqual(result, "")) {
	 result = word ;
      } else {
	 result = Concat(result, Concat(" ", word)) ;
      }
   } /* End of while loop */

   printf("Pig Latin: %s\n", result) ;
}


/* Function: FindWord(string input, int pos) ;

   This function returns the position of the first
   non-space character in the string input, starting
   at position pos.
*/

int FindWord(string input, int pos) {
   int length, i ;

   length = StringLength(input) ;

   /* increment i until a non-space is found */
   for (i = pos ; i < length ; i++) {
      if (!isspace(IthChar(input,i))) return(i) ;
   }

   /* non-space character not found */
   return(-1) ;
}


/* Function: WordEnd(string input, int pos) ;

   This function returns the position of the last
   character in the word in the string input starting 
   at position pos.
*/

int WordEnd(string input, int pos) {
   int length, i ;

   length = StringLength(input) ;

   /* increment i until space is found, then
      return previous non-space character's position */

   for (i = pos + 1 ; i < length ; i++) {
      if (isspace(IthChar(input,i))) return (i-1) ;
   }

   /* if end of string is reached return 
      position of last character*/

   return(length-1) ;
}


/* Function: Isvowel(char c) ;

   Returns true if the given character is
   a vowel.
*/
bool IsVowel(char c) {
   
   c = tolower(c) ;

   if ( (c == 'a') || (c == 'e') || (c == 'i') ||
	(c == 'o') || (c == 'u') ) {
      return(TRUE) ;
   } else {
      return(FALSE) ;
   }
}


/* Function: FirstVowel(string word) ;

   Returns the position of the first vowel in the word.
   If no vowel is found, the function returns -1. 
*/
int FirstVowel(string word) {
   int i, length ;

   length = StringLength(word) ;

   /* increment i until a vowel is found */
   for (i = 0 ; i < length ; i++) {
      if (IsVowel(IthChar(word,i))) return(i) ;
   }

   /* if no vowel is found, ... */
   return(-1) ;
}


/* Function: Translate(string word) ;

   Returns the Pig Latin translation of the word.
*/

string Translate(string word) {
   int vowel_pos, length ;
   string prefix, suffix ;

   word = ConvertToLowerCase(word) ;

   length = StringLength(word) ;
   vowel_pos = FirstVowel(word) ;

   if (vowel_pos == 0) {
      /* word starts with a vowel */
      return(Concat(word,"way")) ;
   } else if (vowel_pos == -1) {
      /* No vowel is found */
      return(Concat(word,"ay")) ;
   } else {
      /* Break up word into prefix and suffix */
      prefix = SubString(word, 0, vowel_pos - 1) ;
      suffix = SubString(word, vowel_pos, length) ;

      /* Return suffix followed by prefix and "ay" */   
      return(Concat(suffix,Concat(prefix,"ay"))) ;
   }
}
