Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Created September 18, 2015 14:06
Show Gist options
  • Save 0x000000AC/bb19ca559299b518d9c2 to your computer and use it in GitHub Desktop.
Save 0x000000AC/bb19ca559299b518d9c2 to your computer and use it in GitHub Desktop.
/*
Pig Latin Translator 0.8
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>
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 will later be
// concatenated with the substring.
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);
}
}
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';
while (choice=='y')
{
printf ("Please enter a word.\n");
scanf ( "%s" , string );
printf ("\nThe String You Entered: %s", string );
upCaseStr(string);
pigLatin (string);
printf (" \nContinue? Press 'y': ");
scanf(" %c", &choice);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment