PROGRAM MUST BE IN C. COMPILER IS XCODE. Declare all prototypes, variables, comm
ID: 3826883 • Letter: P
Question
PROGRAM MUST BE IN C. COMPILER IS XCODE.
Declare all prototypes, variables, comments, etc.
Program – English to Pig Latin
My code is not compiling correctly and no matter what I change it is NOT working. Please help.
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.
BELOW IS THE "pigLatinIn.txt" text
go
placidly
amid
the
noise
and
haste
and
remember
what
peace
there
may
be
in
silence
as
far
as
possible
without
surrender
be
on
good
terms
with
all
persons
speak
your
truth
quietly
and
clearly
and
listen
to
others
even
the
dull
and
the
ignorant
they
too
have
their
story
if
you
compare
yourself
with
others
you
may
become
vain
or
bitter
for
always
there
will
be
greater
and
lesser
persons
than
yourself
enjoy
your
achievements
as
well
as
your
plans
be
yourself
especially
do
not
feign
affection
beyond
a
wholesome
discipline
be
gentle
with
yourself
you
are
a
child
of
the
universe
no
less
than
the
trees
and
the
stars
you
have
a
right
to
be
here
and
whether
or
not
it
is
clear
to
you
no
doubt
the
universe
is
unfolding
as
it
should
be
cheerful
strive
to
be
happy
Explanation / Answer
Here is the code for the question. Output for the input words in the question is also shown . Please don't forget to rate the answer if it helped. Thank you very much.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /*for tolower() */
#include <string.h>
/*converts an english word in *engstr to pig latin word and store the result in pLatinStr. Returns the pig latin word */
char * convertToPigLatin (char * engStr, char * pLatinStr)
{
int len=strlen(engStr), vowelIdx, i, j;
char ch;
/*get the index of the first vowel*/
for(i = 0; i < len; i++)
{
ch = tolower(engStr[i]);
/*if the current character is a vowel, then break out of loop */
if(ch == 'a' || ch =='e' || ch == 'i' || ch == 'o' || ch == 'u')
{
break;
}
}
vowelIdx = i;
/*copy all characters starting from vowelidx in the latin word first, j is the index into latin word and i is the index into english word*/
for(i=vowelIdx, j=0; i<len; i++, j++)
{
pLatinStr[j] = tolower(engStr[i]);
}
/* append a hyphen */
pLatinStr[j++] = '-';
/*if the vowel did not begin in the beginning of english word, then there is some consonant string before, copy that consonant string*/
if(vowelIdx != 0)
{
/* start from begginnig till we reach vowel index*/
for(i = 0; i < vowelIdx; i++,j++)
{
pLatinStr[j] = tolower(engStr[i]);
}
/*append ay after the consonant string is copied*/
pLatinStr[j++]='a';
pLatinStr[j++]='y';
}
else /*there is no consonant string, so append "way" */
{
pLatinStr[j++]='w';
pLatinStr[j++]='a';
pLatinStr[j++]='y';
}
/*put the string terminator*/
pLatinStr[j]='';
return pLatinStr;
}
int main()
{
FILE *infile, *outfile;
char engword[20],piglatin[25]; /*assuming there are maximum 20 characters in an english word*/
/*open input and output files and check if no errors occured while opening*/
infile = fopen("pigLatinIn.txt","r");
if(!infile)
{
printf(" Could not open input file !");
exit(1);
}
outfile = fopen("pigLatinOut.txt","w");
if(!outfile)
{
printf(" Could not open output file !");
exit(1);
}
fprintf(outfile, "English Word Pig Latin Word");
fprintf(outfile, " ___________________________________ ");
/*process the input file*/
while(!feof(infile))
{
fscanf(infile,"%s",engword);
/*convert to pig latin and then write to output file*/
convertToPigLatin(engword,piglatin);
/*print to output file in 20 column width and left aligned*/
fprintf(outfile," %-20s %-20s",engword, piglatin);
}
/*close input and output file*/
fclose(infile);
fclose(outfile);
}
output file pigLatinOut.txt
English Word Pig Latin Word
___________________________________
go o-gay
placidly acidly-play
amid amid-way
the e-thay
noise oise-nay
and and-way
haste aste-hay
and and-way
remember emember-ray
what at-whay
peace eace-pay
there ere-thay
may ay-may
be e-bay
in in-way
silence ilence-say
as as-way
far ar-fay
as as-way
possible ossible-pay
without ithout-way
surrender urrender-say
be e-bay
on on-way
good ood-gay
terms erms-tay
with ith-way
all all-way
persons ersons-pay
speak eak-spay
your our-yay
truth uth-tray
quietly uietly-qay
and and-way
clearly early-clay
and and-way
listen isten-lay
to o-tay
others others-way
even even-way
the e-thay
dull ull-day
and and-way
the e-thay
ignorant ignorant-way
they ey-thay
too oo-tay
have ave-hay
their eir-thay
story ory-stay
if if-way
you ou-yay
compare ompare-cay
yourself ourself-yay
with ith-way
others others-way
you ou-yay
may ay-may
become ecome-bay
vain ain-vay
or or-way
bitter itter-bay
for or-fay
always always-way
there ere-thay
will ill-way
be e-bay
greater eater-gray
and and-way
lesser esser-lay
persons ersons-pay
than an-thay
yourself ourself-yay
enjoy enjoy-way
your our-yay
achievements achievements-way
as as-way
well ell-way
as as-way
your our-yay
plans ans-play
be e-bay
yourself ourself-yay
especially especially-way
do o-day
not ot-nay
feign eign-fay
affection affection-way
beyond eyond-bay
a a-way
wholesome olesome-whay
discipline iscipline-day
be e-bay
gentle entle-gay
with ith-way
yourself ourself-yay
you ou-yay
are are-way
a a-way
child ild-chay
of of-way
the e-thay
universe universe-way
no o-nay
less ess-lay
than an-thay
the e-thay
trees ees-tray
and and-way
the e-thay
stars ars-stay
you ou-yay
have ave-hay
a a-way
right ight-ray
to o-tay
be e-bay
here ere-hay
and and-way
whether ether-whay
or or-way
not ot-nay
it it-way
is is-way
clear ear-clay
to o-tay
you ou-yay
no o-nay
doubt oubt-day
the e-thay
universe universe-way
is is-way
unfolding unfolding-way
as as-way
it it-way
should ould-shay
be e-bay
cheerful eerful-chay
strive ive-stray
to o-tay
be e-bay
happy appy-hay
happy appy-hay
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.