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

PROGRAM MUST BE IN C Program – English to Pig Latin Introduction As most of you

ID: 3693664 • Letter: P

Question

PROGRAM MUST BE IN C

Program – English to Pig Latin

Introduction

As most of you know, pig Latin is a language game played with English words. It is based on discriminating between vowels and consonants within the word. (Note that a “vowel” is any of the characters ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’. A “consonant” is any non-vowel character.)

Any English word can be converted to its pig Latin equivalent by using the following algorithm:

1) Starting from the first character of the word, locate the position of the first vowel.

2) If there is a consonant (or a string of consonants) to the left of that position, do the following:

a) Extract the leading consonant(s) from the word. What remains will begin with a vowel by definition. Let’s call that the “residual string.”

b) Append an “ay” to the end of the consonant string creating a “suffix string”.

c) Append the suffix string to the end of the residual string using a hyphen.

3) If the first letter of the word is a vowel, then append “-way” to the end of the word.

For example, let’s say we want to convert the word “cabin” to pig Latin. Following the above steps, we do the following:

1) We locate the position of the first vowel. This is the ‘a’ that occurs at index 1 in the string (i.e., it is the second letter in the string).

2) Since there is a leading consonant in this case, ‘c’, it is removed from the word creating the residual string = “abin”.

a) We take the consonant, ‘c’, append an “ay” to it, creating the suffix string = “cay”.

b) We append that to the end of the residual string using a hyphen.

3) The final result is “abin-cay”.

Similarly, let’s say that we want to convert the word “apple” to pig Latin. Following the above steps, we do the following:

1) Locate the position of the first vowel. Since it is at index position 0, it is the first character in the string.

2) Since the first character of the word is a vowel, there are no leading consonants to peel off. Therefore, we simply append a “-way” to the end.

3) The final result is “apple-way”.

Assignment

For this assignment, you will write a function that converts a single English word to its pig Latin equivalent. That function should have a header as follows:

char * convertToPigLatin (char * engStr, char * pLatinStr)

You’ll note that it takes two char * as input and returns a char *, as expected. The two inputs are pointers to the English string and the converted Pig Latin string respectively. The return value should be a pointer to the Pig Latin string. That return value can then be assigned to a variable and used to print out the converted string, either to the console or to a file.

Your program should then perform the following steps:

1) Open a file for input called pigLatinIn.txt, which will be provided to you. This file will consist of one English word per line.

2) Open a file for output called pigLatinOut.txt.

3) Your program will then read the input file, use the convertToPigLatin() function to determine the pig Latin equivalent to the word, then write the following to the output file:

English Word Pig Latin Word

------------ --------------

cabin abin-cay

apple apple-way

4) Print out the results in all lower case.

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int first_vowel(char *ch,int len)
{
int i;
for(i=0;i<len;i++)
{
if(ch[i]=='a' || ch[i]=='e' || ch[i]=='i' || ch[i]=='o' || ch[i]=='u')
{
return i;
}
}
return i;
}
char * convertToPigLatin (char * engStr, char * pLatinStr)
{
int i,j,k;
int len = strlen(engStr);
int fvw = first_vowel(engStr,len);
if(fvw==0)
{

for(i=0;i<len;i++)pLatinStr[i] = engStr[i];
pLatinStr[i] = '-';i++;
pLatinStr[i] = 'w';i++;
pLatinStr[i] = 'a';i++;
pLatinStr[i] = 'y';i++;
pLatinStr[i]='';
}
else
{
for(i=fvw,j=0;i<len;i++,j++)pLatinStr[j] = engStr[i];
pLatinStr[j]='-';j++;
for(i=0;i<fvw;i++,j++)pLatinStr[j] = engStr[i];
pLatinStr[j]='a';j++;
pLatinStr[j]='y';j++;
pLatinStr[j]='';
}
return pLatinStr;
}
void makesmall(char * word)
{
int len = strlen(word);
int i;
for(i=0;i<len;i++)
{
if(word[i]>='A' && word[i]<='Z')
{
word[i] = word[i]-'A'+'a';
}
}
}
int main(void)
{
char *example,*ans;
char word[1000];
int len;
FILE *fp,*fp1;
fp = fopen("pigLatinIn.txt", "r");
fp1 = fopen("pigLatinOut.txt", "w");
while(fscanf(fp, "%s", word)!=EOF)
{
makesmall(word);
int len = strlen(word);
example = (char *)malloc(sizeof(char)*(len+5));
ans = convertToPigLatin(word,example);
fprintf(fp1,"%s ",ans);
}
fclose(fp1);
fclose(fp);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote