Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 5.2 (design, piglatin.py). Before attempting to code this problem, crea

ID: 3705608 • Letter: E

Question

Exercise 5.2 (design, piglatin.py). Before attempting to code this problem, create a file design that contains some analysis of how you think the problem will be solved. Examples include but are not limited to: a flowchart of events in the program, pseudocode, or a step-by-step process written in plain English. If you choose to scan your design, please make sure that it is legible. Write a program that translates a file from English to pig latin. The rules for pig latin are as follows: For a word that begins with consonants, the initial consonant or consonant cluster is moved to the end of the word and "ay" is added as a suffix: "happy""appyhay" ."glove""oveglay For words that begin with vowels, you add "way" to the end of the word: egg""eggway "inbox""inboxway For your program, you must write a function that takes in one individual word and returns the translation to pig latin. Write another function that takes a string, which may be sentences (may contain the characters "a-zA-Z 20" and space), and returns the translation of the sentence to pig latin. Strip out any punctuation. For example, "Hello, how are you?" would translate into "elloHay owhay areway ouyay The user must be able to specify the filename for the file to be translated and the filename that the program should write to. For example: CSE/IT 1071 Lab 10 Review and Markov Chain ??cat test.txt 2 Hello, how are you? ? $ python3 piglatin.py Enter English filenane >>> test.txt s Enter filenane to vrite to >>> test piglatin.txt s Done $ cat test.piglatin.txt s elloHay owhay arevay ouyay

Explanation / Answer

#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(" Pig Latin Translator in C ");

    printf( "Type 'END' when finished ");

    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(" aveHa aa icena ayda (Have a nice day) ");

}

/* 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()) != ' ')

    {

        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(" ");

    return;

}

#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(" Pig Latin Translator in C ");

    printf( "Type 'END' when finished ");

    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(" aveHa aa icena ayda (Have a nice day) ");

}

/* 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()) != ' ')

    {

        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(" ");

    return;

}