Hello,
This is my first time on any kind of programming forum, so please forgive me if I violate any rules. As with most students, I have been tasked with converting a string from English to Piglatin.
Below are the exact requirements of the project (sorry for the length):
Overview:
This project is for testing the use of structures, arrays and string handling functions. In this assignment you will be prompting the user to enter an english sentence. The program will then convert it to the code Pig Latin . Your program will then output this Pig Latin to the screen.
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: oodfellasgay 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.
Currently, I've been able to display each individual sub-string; however, I'm unsure how to actually produce the function that does displays the actual translation. I've tried every resource at my disposal and regardless of what I try, the program won't compile. Please see my current code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
struct Word
{
string english;
string piglatin;
};
Word * split_sent(string sentSubs, int &size);
int main ()
{
string userSent;
int size;
cout << "Please enter a sentence :\n";
getline (cin, userSent);
Word *pSent = split_sent (userSent, size);
for (int i = 0; i < size; ++i)
{
cout << pSent[i].english << endl;
}
delete[]pSent;
return 0;
}
Word * split_sent(string sentSubs, int &size)
{
bool flag = true;
int numbers = 0;
char sub;
for (int i = 0; i < sentSubs.length (); ++i)
{
sub = sentSubs[i];
if (isalpha (sub))
{
if (flag)
{
flag = false;
++numbers;
}
}
else if (isspace (sub))
{
flag = true;
}
}
Word *sent = new Word[numbers];
int index = -1;
flag = true;
numbers = 0;
for (int i = 0; i < sentSubs.length (); ++i)
{
sub = sentSubs[i];
if (isalpha (sub))
{
if (flag)
{
flag = false;
++numbers;
++index;
}
if (isupper (sub))
{
sub = tolower (sub);
}
sent[index].english += sub;
}
else if (isspace (sub))
{
flag = true;
}
}
size = numbers;
return sent;
}
|
I would appreciate any constructive feedback, and of course any tips, resources, pointers on how my current code could be made more simple and produce the same results. Again, sorry for the length of my enquiry, and have a good evening.
Kind regards,
Novice 88