/* Title : LATIN.C LastEdit: 07/20/88 Author : Kim Moser System : Turbo C 2.0 Descrip : Reads standard input and writes Pig Latin or Egg Latin equivalent to standard output. */ #include #include #include #include #define PigLatin 1 #define EggLatin 2 #define MAX_WORD_LEN 512 static int Language; static int VowelCount; static int isletter(int ch); static int isletter(int ch) /* Returns whether 'ch' is a letter in a word */ { return ((isalpha(ch)) || (ch=='\'')); } static int isvowel(int ch); static int isvowel(int ch) /* Returns whether 'ch' is a vowel */ { return (strchr("AEIOUY", toupper(ch)) != NULL); } static void usage(void); static void usage(void) { fputs("LATIN v2.0 (24 May 1990) (c) Copyright 1990 Kim Moser All Rights Reserved\n", stderr); fputs("Translates standard input to either Pig Latin or Egg Latin,\n", stderr); fputs("depending on command line switch used.\n", stderr); fputs("\nUsage: LATIN -p | -e", stderr); exit(-1); } static void chrcat(char *dest, int ch); static void chrcat(char *dest, int ch) /* Appends 'ch' to 'dest' */ { int i; dest[i=strlen(dest)] = ch; dest[i+1] = '\0'; } static void translate(char *in, char *out); static void translate(char *in, char *out) { int i; char TempWord[MAX_WORD_LEN+1]; i = 0; /* Index into 'in' */ out[0] = '\0'; /* Properly initialize string */ switch (Language) { case PigLatin: TempWord[0] = 0; /* Properly initialize string */ while (1) { /* Find first vowel ['Y' does NOT count if it's the first letter of the word]: */ while ((in[i]) && (!(isvowel(in[i])))) { chrcat( TempWord, tolower(in[i]) ); i++; } if ((toupper(in[i]) == 'Y') && (!i)) { /* Leading 'Y' does NOT count as a vowel */ chrcat( TempWord, tolower(in[i]) ); /* Will always be 'y' */ i++; } else { break; } } /* Put rest of 'word' in 'out': */ while (in[i]) { chrcat(out, in[i]); i++; } strcat(out, TempWord); strcat(out, "ay"); break; case EggLatin: VowelCount = 0; /* No vowels found [yet] */ out[0] = 0; /* Properly initialize string */ do { /* Process all vowels: */ /* Find next vowel: */ while ((in[i]) && ( (!isvowel(in[i])) || /* Leading 'Y' does NOT count as a vowel: */ ((toupper(in[i])=='Y') && (!i)) || /* Trailing vowel does NOT count as vowel IFF there were previous vowels: */ ((isvowel(in[i])) && (!in[i+1]) && (VowelCount > 0)) || /* Ignore vowel if previous char was vowel, too: */ ((i>1) && (isvowel(in[i-1]))) )) { chrcat(out, in[i]); i++; } if (in[i]) { /* Vowel was found [we're not at end of word] */ VowelCount++; strcat( out, "egg" ); chrcat( out, in[i] ); i++; } } while (in[i]); break; default: fprintf(stderr, "LATIN: Internal error; unknown language to translate to.\n"); exit(-1); } } static int readword(char *dest); static int readword(char *dest) /* Parses a "word" [any sequence of alpha chars, delimited by leading and/or trailing blanks] from standard input into 'w'. Returns TRUE if length of word is > 0, else returns FALSE. */ { int i = 0; int ch = '\0'; /* Find beginning of word: */ while ((!feof(stdin)) && (!isletter(ch))) { /* WriteStr( outp, ch ); */ ch = getchar(); } /* Parse until end of word: */ while (! (feof(stdin)) && (isletter(ch))) { dest[i] = ch; i++; ch = getchar(); } dest[i] = '\0'; /* Properly terminate word */ return (i); } void main(int argc, char **argv); void main(int argc, char **argv) { int i; char Word[MAX_WORD_LEN+1]; char Word2[MAX_WORD_LEN+1]; if (argc == 1) { usage(); } else { if (((i=toupper(argv[1][1])) == 'P')) { Language = PigLatin; } else if (i == 'E') { Language = EggLatin; } else { usage(); } while (!feof(stdin)) { if (readword(Word)) { translate( Word, Word2 ); /* Capitalize first letter, if supposed to be: */ if (toupper(Word[0]) == Word[0]) { Word2[0] = toupper(Word2[0]); } } puts(Word2); } } }