Question

In: Computer Science

IN C++ Note: While there are many ways to do conversions to pig latin, I will...

IN C++

Note: While there are many ways to do conversions to pig latin, I will require that you follow the procedures below, all of which will use the following structure:

struct Word {
 string english;
 string piglatin;
};

Part 1. Write a function that takes in an English sentence as one string. This function should first calculate how many “words” are in the sentence (words being substrings separated by whitespace). It should then allocate a dynamic array of size equal to the number of words. The array contains Word structures (i.e. array of type Word). The function would then store each word of that sentence to the english field of the corresponding structure. The function should then return this array to the calling function using the return statement, along with the array size using a reference parameter.

This function should also remove all capitalization and special characters other than letters. Implement the function with the following prototype

Word * splitSentence(const string words, int &size);

Part 2. Write a function that takes in an array of Word structures and the size of the array and converts each english field to the corresponding piglatin field.

void convertToPigLatin(Word [] wordArr, int size);

To do this conversion, if a word starts with a consonant, the piglatin conversion of the word involves moving the first letter of the word to the end of the string and then adding “ay” to the end.

pig -> igpay

cat -> atcay

dog -> ogday

If the word starts with a vowel, simply add “way” to the end of the word

apple -> appleway

are -> areway

Part 3. Write a function that takes in an array of Word structures and outputs the pig latin part of it to the screen, with each word separated by a space.

void displayPigLatin(const Word [] wordArr, int size);

Example:

Please enter a string to convert to PigLatin:
Casino is nothing but a Goodfellas knockoff
Output:
asinocay isway othingnay utbay away oodfellasgay nockoffkay 

Error conditions: Your program should get rid of all punctuation and special characters other than letters. Your program should be able to deal with there being two or more spaces between words.

Note: Make sure to follow proper programming style, as per the style supplement.

Solutions

Expert Solution

#include<bits/stdc++.h>
#include <iostream>
using namespace std;

struct Word {
  string english;
  string piglatin;
};

string validate(string s) {
  string ans;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] >= 'a' && s[i] <= 'z') {
      ans += s[i];
    } else if (s[i] >= 'A' && s[i] <= 'Z') {
      ans += tolower(s[i]);
    }
  }
  return ans;
}

Word * splitSentence(const string words, int &size) {

  // breaking input into word using string stream
  stringstream s(words); // Used for breaking words
  string word; // to store individual words

  int count = 0;
  while (s >> word)
    count++;

  size = count;

  struct Word* array = (struct Word*)malloc(size * sizeof(struct Word));
  int i = 0;
  stringstream ss(words);
  while (ss >> word) {
    array[i].english = validate(word);
    i++;
  }

  return array;

}

void convertToPigLatin(Word  wordArr[], int size) {

  for (int i = 0; i < size; ++i) {

    char c = wordArr[i].english[0];
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
      string ans = wordArr[i].english + "way";
      wordArr[i].piglatin = ans;
    } else {
      string ans = wordArr[i].english;
      ans.erase(ans.begin());
      ans += wordArr[i].english[0];
      ans += "ay";
      wordArr[i].piglatin = ans;
    }
  }
}

void displayPigLatin(const Word  wordArr[], int size) {

  for (int i = 0; i < size; ++i) {
    cout << wordArr[i].piglatin << " ";
  }
  cout << endl;
}

int main() {

  string input_string;
  getline(cin, input_string);
  int n;

  Word* array = splitSentence(input_string, n);

  convertToPigLatin(array, n);

  displayPigLatin(array, n);

  return 0;
}

OUTPUT:


Related Solutions

C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console...
C++ Project 6-2: Pig Latin Translator Create a program that translates English to Pig Latin. Console Pig Latin Translator This program translates a sentence and removes all punctuation from it. Enter a sentence: 'Tis but a scratch. Translation:      Istay utbay away atchscray Specifications Convert the English to lowercase before translating. Remove all punctuation characters before translating. Assume that words are separated from each other by a single space. If the word starts with a vowel, just add way to the...
How do I write a COBOL program that translates a word entered into pig Latin?
How do I write a COBOL program that translates a word entered into pig Latin?
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i...
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i <= 0) { cout << "!"; --i; }
How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3};...
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3}; (NOTE: please consider braces {} here as usual square brackets for array index). Assume that i is stored in register $s1, and the base address of the array A is in $s2. Fill in the multiple blank spaces in the following MIPS program that is supposed to be compiled from the above C loop: loop: sll $t0, $s1,                add $t0, $t0,                lw $t1, 20($s2)...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
how do marketers create needs. note** state 4 ways they do create needs
how do marketers create needs. note** state 4 ways they do create needs
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT