Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Created September 7, 2015 02:07
Show Gist options
  • Save 0x000000AC/ac8c913f786b88eb7e09 to your computer and use it in GitHub Desktop.
Save 0x000000AC/ac8c913f786b88eb7e09 to your computer and use it in GitHub Desktop.
/*
Pig Latin Translator 0.6
User Enters a word after the prompt
and gets the "Pig Latin" version back at the
prompt.
Compilation: #cc pigLatin.c -o pigLatinProgram
Execution: #./pigLatinProgram
Example:
apoh@apohGP:~/Desktop$ ./pigLatinProgram
Please enter a word.
Aaron
The String You Entered: Aaron
This is the substring: aron
The first character of the word is A
AyEnding: ay
Your word in Pig Latin is aronAay
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";
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[1];
memcpy ( wordFirstChar, &word[0], 1 );
wordFirstChar[1] = '\0';
// 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] = '\0'; // must ensure null terminator is re-added
// You have the char, you have the substring, concatenate
// the two and append 'ay'
char pigLatinifiedWord[charCount+3];
strcat(pigLatinifiedWord, subString);
strcat(pigLatinifiedWord, wordFirstChar);
strcat(pigLatinifiedWord, ayEnding);
printf("\nThis is the substring: %s\n", subString);
printf("The first character of the word is %s\n", wordFirstChar);
printf("AyEnding: %s\n", ayEnding);
printf("Your word in Pig Latin is %s\n", pigLatinifiedWord);
}
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