Write a program that encodes English-language phrase, tokenize the phrase into w
ID: 3688366 • Letter: W
Question
Write a program that encodes English-language phrase, tokenize the phrase into words with function strtok. To translate each English word into a pig-latin word, place the first letter of the English word at the end of the English word and add the letters "ay". Thus the word "jump" becomes "umpjay", the word "the" become "hetay" and the word "computer" becomes "omputercay". Blacks between words remain as blanks. Assume the following: The English phrase consists of words separated by blanks, there are no punctuation marks, and all words have two or more letters. The sample program execution is as follows: Enter your phrase: hello happy birthday joey Pig-Latin phrase is: ellohay appyhay irthdaybay oeyjay For this question, you are free to define you own functions and use them in the main function. For this question, you need to write your program in file, called question2 c.Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void init(char english[], char piglatin[]);
void read(char english[]);
int count(char english[]);
void convert(int words, char english[], char piglatin[]);
void print(char piglatin[]);
int main( )
{
char english[80], piglatin[80];
int words;
printf(" Pig Latin Translator in C ");
do
{
/* processing a new line of text */
init(english, piglatin);
read(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 = count(english);
/* Now Pig Latin Translator in C converts English to Pig Latin */
convert(words, english, piglatin);
print(piglatin);
}
while (words >= 0);
return 0;
}
/* initializing character arrays with blank spaces */
void init(char english[], char piglatin[])
{
int count;
for (count = 0; count < 80; ++count)
english[count] = piglatin[count] = ' ';
return;
}
void read(char english[])
{
int count = 0;
char c;
while (( c = getchar()) != ' ')
{
english[count] = c;
++count;
}
return;
}
/* determining the number of words */
int count(char english[])
{
int count, words = 1;
for (count = 0; count < 79; ++count)
if (english[count] == ' ' && english[count + 1] != ' ')
++words;
return (words);
}
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' */
piglatin[m2 + (n+1)] = 'y';/* adding 'y' at the end*/
/* reseting the initial marker */
m1 = m2 + 2;
}
return;
}
void print(char piglatin[])
{
int count = 0;
for (count = 0; count < 80; ++count)
putchar(piglatin[count]);
printf(" ");
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.