Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Created October 9, 2015 19:21
Show Gist options
  • Save 0x000000AC/e104c0daa365a9f74155 to your computer and use it in GitHub Desktop.
Save 0x000000AC/e104c0daa365a9f74155 to your computer and use it in GitHub Desktop.
/*
Pig Latin Translator 1.0.2
User Enters a word after the prompt
and gets the "Pig Latin" version back at the
prompt.
Example Compilation and Run
Apoh:Documents apoh$ cc pigLatinTranslator.c -o pigLatin
Apoh:Documents apoh$ ./pigLatin
Please enter a word.
Tappas
The String You Entered: Tappas
Your word in Pig Latin is APPASTAY
Continue? Type 'y' followed by enter to continue or any other character to end:
Note: Runs natively on *nix based systems. Needs cygwin 32-bit for Windows
Author: EST Group CS380
*/
#include <stdio.h>
#include <string.h>
#include <regex.h>
char pigLatin ( char *word )
{
// First, get a count of the number
// of characters in the user-input string
int n;
int charCount = 0;
char *ayEnding = "AY";
char *yayEnding = "YAY";
for( n=0; word[n]; ++n)
{
if( word[n] != ' ' )
{
charCount ++;
}
}
// Create a substring with the first two characters of
// the user-entered word. This is then used as a test
// for a consonant/double-consonant or vowel beginning word.
char wordFirstTwoChars[2];
memcpy ( wordFirstTwoChars, &word[0], 2);
wordFirstTwoChars[2] = '\0';
if (wordFirstTwoChars[0]=='A' || wordFirstTwoChars[0]=='E' || wordFirstTwoChars[0]=='I'
|| wordFirstTwoChars[0]=='O' || wordFirstTwoChars[0]=='U' )
{
// If first letter is a vowel, just add 'yay'
char pigLatinVowel[charCount+2];
strcpy(pigLatinVowel, word);
strcat(pigLatinVowel, yayEnding);
pigLatinVowel[charCount+3] = '\0';
printf("\nYour Word in Pig Latin is %s\n", pigLatinVowel);
}
else if (wordFirstTwoChars[1] == 'H' || wordFirstTwoChars[1] =='C' || wordFirstTwoChars[1] =='L' || wordFirstTwoChars[1] =='R' || wordFirstTwoChars[1] =='H'
|| wordFirstTwoChars[1] =='K' || wordFirstTwoChars[1] =='M' || wordFirstTwoChars[1] =='N' || wordFirstTwoChars[1] =='P' || wordFirstTwoChars[1] =='Q'
|| wordFirstTwoChars[1] =='T' || wordFirstTwoChars[1] =='T')
{
// For double consonant words i.e., the, character, thomas etc
char subString[charCount];
memcpy( subString, &word[2], charCount-1 );
subString[charCount-1] = '\0'; // must ensure null terminator is re-added
// You have the char, you have the substring, concatenate
// the two and append 'ay'
char pigLatinConsonant[charCount+2];
strcpy(pigLatinConsonant, subString);
strcat(pigLatinConsonant, wordFirstTwoChars);
strcat(pigLatinConsonant, ayEnding);
pigLatinConsonant[charCount+3] = '\0';
printf("\nYour word in Pig Latin is %s\n", pigLatinConsonant);
}
else
{
// If the word doesn't begin with a vowel, append
// the first letter with 'ay'
/* Declare a substring with the number of characters
from the string. This will be your base to which
the first character of the original string and
'ay' will be appended */
char subString[charCount];
memcpy( subString, &word[1], charCount-1 );
subString[charCount-1] = '\0'; // must ensure null terminator is re-added
// You have the char, you have the substring, concatenate
// the two and append 'ay'
char pigLatinConsonant[charCount+2];
strcpy(pigLatinConsonant, subString);
// Take the first letter from original word first-two letter string
char wordFirstChar[1];
memcpy (wordFirstChar, &wordFirstTwoChars[0], 1);
wordFirstChar[1] = '\0';
strcat(pigLatinConsonant, wordFirstChar);
strcat(pigLatinConsonant, ayEnding);
pigLatinConsonant[charCount+3] = '\0';
printf("\nYour word in Pig Latin is %s\n", pigLatinConsonant);
}
}
// Helper method to change the string to all caps
void upCaseStr(char *s)
{
char *p;
for (p = s; *p != '\0'; p++)
*p = (char) toupper(*p);
}
int main (void)
{
/* Prototype declaration for pigLatin function
This tells the compiler that pigLatin returns a char
and takes and array of 50 chars. Not really necessary
since pigLatin is declared before main, but playing
it safe */
char pigLatin (char *string);
char string[256]; // The user entered word
char choice = 'y';
// Regular expression begins with any upper or lower
// and only contains them as well. String is arbitrarily
// long.
regex_t reg;
const char *regex="^[A-z]+$";
regmatch_t matches[256];
regcomp(&reg, regex, REG_EXTENDED); // Must compile the regex before use
while (choice=='y')
{
printf ("Please enter a word.\n");
scanf ( "%s" , string );
// Start with ensuring first character of the user string is A-Z or a-z by comparing
// to the ASCII value for those letters.
if ( ( string[0] > 64 && string[0] < 91 ) || ( string[0] > 96 && string[0] < 123 ) )
{
// Now check to ensure the entire word is a contiguous string of characters
if(regexec(&reg, string, 256, matches, 0) == 0)
{
printf ("\nThe String You Entered: %s", string );
upCaseStr(string);
pigLatin (string);
printf (" \nContinue? Type 'y' followed by enter to continue or any other character to end: ");
scanf(" %c", &choice);
if (choice!='y')
{
break;
}
}
else
{
printf("\nYour word must contain only letters\n");
choice = 'y';
}
}
else
{
printf ("\nYour word doesn't have a valid starting character.\n\n");
choice = 'y';
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment