Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Created September 20, 2015 03:02
Show Gist options
  • Save 0x000000AC/0f1edfd1d6b54c68d4f3 to your computer and use it in GitHub Desktop.
Save 0x000000AC/0f1edfd1d6b54c68d4f3 to your computer and use it in GitHub Desktop.
/*
Pig Latin Translator 0.8.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? Press 'y':
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 character of
// the user-entered word. This is then used as a test
// for a consonant or vowel beginning word.
char wordFirstChar[2];
memcpy ( wordFirstChar, &word[0], 1 );
wordFirstChar[1] = '\0';
if (wordFirstChar[0]=='A' || wordFirstChar[0]=='E' || wordFirstChar[0]=='I'
|| wordFirstChar[0]=='O' || wordFirstChar[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 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);
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);
}
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