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

Programming In C language, NOT C++ Important: Program in C language, not in C++.

ID: 3824945 • Letter: P

Question

Programming In C language, NOT C++

Important: Program in C language, not in C++. Find the contents of the required file below piglatinin.txt:

Program 1 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: l) 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.

Explanation / Answer

Here is the code for you:

#include <stdio.h>
#include <string.h>

int posFirstVowel(char inStr[])
{
    for(int i = 0; i < strlen(inStr); i++)
    {
       switch(inStr[i])
       {  
          case 'A':
          case 'a':
          case 'E':
          case 'e':
          case 'I':
          case 'i':
          case 'O':
          case 'o':
          case 'U':
          case 'u':   return i;
       }
    }
    return -1;
}
void convertToPigLatin(char inStr[], char pigLatin[])
{
    int firstVowelPosition = posFirstVowel(inStr);
    if(firstVowelPosition == 0)
    {
        strcpy(pigLatin, inStr);
        strcat(pigLatin, "-way");
    }  
    else
    {
       char residualString[30];
       strcpy(residualString, inStr + firstVowelPosition);
       char consString[30];
       strcpy(consString, inStr);
       consString[firstVowelPosition] = '';
       strcat(residualString, "-");
       strcat(residualString, consString);
       strcat(residualString, "ay");
       //return residualString + "-" + consString + "ay";
    }  
}
int main()
{
    FILE *fin = fopen("pigLatinIn.txt", "r");
    if(fin == NULL)
    {
       printf("Unable to open the input file... Quitting..... ");
       return 0;
    }
    FILE *fout = fopen("pigLatinOut.txt", "w");
    if(fout == NULL)
    {
       printf("Unable to open the output file... Quitting..... ");
       return 0;
    }
    fprintf(fout, " English word Pig Latin word ");
    //fout << " English word Pig Latin word" << endl;
    while(!feof(fin))
    {
       char word[30], pigLatinWord[30];
       fscanf(fin, "%s", word);
       //fin >> word;
       convertToPigLatin(word, pigLatinWord);
       fprintf(fout, "%15s %15s ", word, pigLatinWord);
       //cout << setw(15) << word << " " << setw(15) << convertToPigLatin(word) << endl;
       //fout << setw(15) << word << " " << setw(15) << convertToPigLatin(word) << endl;
    }  
}