In Language C using Visual Studio only.. 1. Declare a string named pigWord that
ID: 3576460 • Letter: I
Question
In Language C using Visual Studio only..
1. Declare a string named pigWord that will be able to store up to 30 characters. Write the code that prompts the user to enter a word and store it in pigWord. Then take the string and turn it into Pig Latin using the following rules:
Rules
I. If the word begins with one of the following two letters TH, CH, SH, PH, WH, or QU then those two letters are removed from the front of the word and added to the end of the word and the letters AY are added after that.
Examples:
II.
WHAT becomes ATWHAY CHURCH becomes URCHCHAY PHONE becomes ONEPHAY QUIT becomes ITQUAY
If the word begins with the letters A, E, I, O, or U then simply add the letters AY to the end of the word.
Examples:
ISLAND becomes ISLANDAY APPLE becomes APPLEAY OCEAN becomes OCEANAY
If the rules 1 and 2 do not apply then the first letter of the word is removed from the front and added to the end and then the letters AY are added to the end of that.
Examples:
HELLO become ELLOHAY PINEAPPLE becomes INEAPPLEPAY
MUSHROOM becomes USHROOMMAY
III.
You should try to use one or more of the string library functions, strlen, strcpy, strcat, or strcmp to change your string.
Explanation / Answer
//main.c
#include <stdio.h>
#include <string.h>
#include "genlib.h"
#include "simpio.h"
#define MaxWord 20
/* Private function prototypes */
static void PigLatin(char *word, char buffer[], int bufferSize);
static char *FindFirstVowel(char *word);
static bool IsVowel(char ch);
/* Main program */
main()
{
string word;
char translationBuffer[MaxWord + 1];
printf("Enter a word: ");
word = GetLine();
PigLatin(word, translationBuffer, MaxWord + 1);
printf("Pig Latin: %s ", translationBuffer);
}
static void PigLatin(char *word, char buffer[], int bufferSize)
{
char *vp;
int wordLength;
vp = FindFirstVowel(word);
wordLength = strlen(word);
if (vp == word) {
wordLength += 3;
} else if (vp != NULL) {
wordLength += 2;
}
if (wordLength >= bufferSize) Error("Buffer overflow");
if (vp == NULL) {
strcpy(buffer, word);
} else if (vp == word) {
strcpy(buffer, word);
strcat(buffer, "way");
} else {
strcpy(buffer, vp);
strncat(buffer, word, vp - word);
strcat(buffer, "ay");
}
}
static char *FindFirstVowel(char *word)
{
char *cp;
for (cp = word; *cp != ''; cp++) {
if (IsVowel(*cp)) return (cp);
}
return (NULL);
}
static bool IsVowel(char ch)
{
switch (ch) {
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case 'u':
return (TRUE);
default:
return (FALSE);
}
}
======================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.