Pig Latin translator, often used by children as a game, is an encoded form of English language. It transposes the first sound or the first letter of an English word to the end of the word, and then adds the letter ‘a‘. So, when the word ‘dog’ is passed through Pig Latin Translator, it becomes ‘ogda‘, ‘computer’ becomes ‘omputerca‘, and so on. The word ‘piglatin’ becomes ‘iglatinpa‘ or ‘igpa atinla‘ if used or spelled as two separate words.
The program for Pig Latin Translator in C presented here will accept a line of text in English, and then give out the corresponding text in Pig Latin as output. In the source code, I have mentioned comments in various sections of the code to help you understand the program better. But before going through the C source code, here, I have enlisted the features and working procedure or computational strategy of this Pig Latin Translator program.
Features of Pig Latin Translator in C:
- The program accepts only a line of text in English, and it will be translated to Pig Latin.
- Each text message can be typed on one 80-column line only, with a single black space between each words.
- The message sent to the program should not exceed 80 characters; it should be less than that as the original message will be lengthened somewhat by Pig Latin Translator.
- For simplicity, the Pig Latin Translator in C transposes only the first letter of each word – not the first sound.
- Capitalization and punctuations are ignored.
Working Procedure of Pig Latin Translator in C:
The program will use two character arrays, namely english and piglatin; one containing the original message, and the other containing the translated Pig Latin text. The overall working procedure of the Pig Latin Translator is simple and straightforward; it follows the following major steps:
- Both arrays are initialized assigning blank spaces to all the elements contained by them.
- Ask for the message or line of text from the user.
- Determine the number of words in the line of text. This is done by counting the number of single black spaces that are followed by a non-blank space.
- Rearrange the words into Pig Latin, following the steps mentioned below for each word.
- Locate the end of the word.
- Then, transpose the first letter to the end of the word, and add an ‘a‘.
- Locate the beginning of the next word, follow above step.
- Display the translated line of text as output.
This program – Pig Latin Translator in C continues the above steps repeatedly until it reads a line of text whose first three letters are ‘END‘ or ‘end‘. To make use of this overall working procedure, two markers, namely m1 and m2 are used in the program. The first marker m1 indicates the position of the beginning of each word, while the second marker m2 indicates the end of the word within the original line of text.
Following are the main program variables used in Pig Latin Translator in C:
- english – a one-dimensional array containing the original message/line of text
- piglatin – a one-dimensional array containing the translated message/line of text
- words – an integer variable that counts the number of words in the given message/line of text
- n – an integer variable used as a word counter
- count – an integer variable that counts character within each line
- m1 – an integer variable that indicates the position of the beginning of each word
- m2 – an integer variable that indicates the end of the word
Pig Latin Translator in C Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void initialize(char english[], char piglatin[]);
void readinput(char english[]);
int countwords(char english[]);
void convert(int words, char english[], char piglatin[]);
void writeoutput(char piglatin[]);
main( )
{
char english[80], piglatin[80];
int words;
printf("\nPig Latin Translator in C\n\n");
printf( "Type \ 'END\' when finished\n\n");
do
{
/* processing a new line of text */
initialize(english, piglatin);
readinput(english);
/* testing stopping condition */
if (toupper(english[0]) == 'E' && toupper(english[1]) == 'N' && toupper(english[2]) == 'D')
break;
/* count the number of words in the line */
words = countwords(english);
/* Now Pig Latin Translator in C converts English to Pig Latin */
convert(words, english, piglatin);
writeoutput(piglatin);
}
while (words >= 0);
printf("\naveHa aa icena ayda (Have a nice day)\n");
}
/* initializing character arrays with blank spaces */
void initialize(char english[], char piglatin[])
{
int count;
for (count = 0; count < 80; ++count)
english[count] = piglatin[count] = ' ';
return;
}
/* reading one line of text in English */
void readinput(char english[])
{
int count = 0;
char c;
while (( c = getchar()) != '\n')
{
english[count] = c;
++count;
}
return;
}
/* scanning the given message or line of text and determining the number of words in it */
int countwords(char english[])
{
int count, words = 1;
for (count = 0; count < 79; ++count)
if (english[count] == ' ' && english[count + 1] != ' ')
++words;
return (words);
}
/* now Pig Latin translator in C coverts each word into Pig Latin */
void convert(int words, char english[], char piglatin[])
{
int n, count;
int m1 = 0; /* m1 indicates the position of beginning of each word */
int m2; /* m2 indicates the end of the word */
/* convert each word */
for (n = 1; n <= words; ++n)
{
/* locating the end of the current word */
count = m1 ;
while (english[count] != ' ')
m2 = count++;
/* transposing the first letter of each word and adding 'a' at the end */
for (count = m1 ; count < m2; ++count)
piglatin[count + (n - 1)] = english[count + 1];
piglatin[m2 + (n - 1)] = english[m1];
piglatin[m2 + n] = 'a'; /* adding 'a' at the end */
/* reseting the initial marker */
m1 = m2 + 2;
}
return;
}
/* now Pig Latin translator in C displays the line of English text in Pig Latin */
void writeoutput(char piglatin[])
{
int count = 0;
for (count = 0; count < 80; ++count)
putchar(piglatin[count]);
printf("\n");
return;
}
Copy the source code for Pig Latin Translator in Code::Blocks IDE using GCC compiler, and you’ll get an output as shown below.
As already stated in the features, the Pig Latin translator in C does not have any special accommodations for double-letter sounds (th, sh), uppercase letters and punctuation marks. These have been left as rooms for improvements in the source code for readers. Your queries and feedback regarding Pig Latin translator can be brought up to me from the comments box below.