Having trouble with English to Pig Latin Translation Project

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
Last edited on
however, I'm unsure how to actually produce the function that does displays the actual translation

I am not sure if you are allowed to add some functions. If yes you could create the following functions.
1
2
3
4
5
6
7
8
9
10
11
12

bool is_consonant(char c);
bool is_vowel(char c);
string to_piglatin(const string& english);

void convertToPigLatin(Word [] wordArr, int size)
{
  for (int i = 0; i < size; ++i)
  {
     wordArr[i].piglatin = to_english(wordArr[i].english;
  }
}


Outputting the translation is similar to outputting the english words on line 24, though you should create the function void displayPigLatin(const Word [] wordArr, int size);
Last edited on
I guess it's not allowed to you using some sort of c++ specific techniques for your task. But for sake of showing good programming style, I made the program how I would it solve. It's feasible rewriting the stuff without using pointers, and even without the need of using some indices.

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
#include <iostream>
#include <cctype>
#include <string>

#include <vector>

using namespace std;

struct Word
{
    Word( const string & en_word = "") { english = en_word; }
    string english;
    string piglatin;
};


void split_sent( string & sentSubs, vector<Word> & wordlist );
void en_word_to_piglatin( const string & en_word, string & piglatin_word );


int main ()
{
    string userSent;
    int size;

    cout << "Please enter a sentence :\n";
    getline (cin, userSent);

    vector<Word> vSent;
    split_sent (userSent, vSent );

    for ( auto & word : vSent)
    {
        en_word_to_piglatin( word.english, word.piglatin );
    }
    for (auto & word : vSent)
    {
        cout << word.english << '\n';
    }
    cout << endl << "piglatin: " << '\n';
    for (auto & word : vSent)
    {
        cout << word.piglatin << ' ';
    }

    // return 0; // not needed, returns by default 0
}

void split_sent( string & sentSubs, vector<Word> & wordlist )
{
    bool space_flag = true;
    std::string en_word;

    for (auto & letter : sentSubs)
    {
        if (isalpha (letter))
        {
            space_flag = false;
            letter = tolower (letter);
            en_word += letter;
        }
        else if (isspace (letter))
        {
            if (space_flag == false)
            {
                wordlist.push_back( Word(en_word) );
                en_word.clear();
            }
            space_flag = true;
        }
    }
    // copies the last word
    if (en_word.empty() == false)
    {
       wordlist.push_back( Word(en_word) );
    }
}

void en_word_to_piglatin( const string& en_word, string & piglatin_word )
{
    for (auto vowel : "aeiou")
    {
        if (en_word[0] == vowel)
        {
            piglatin_word = en_word;
            piglatin_word += "way";
            return;
        }
    }

    piglatin_word = en_word.substr(1,en_word.npos) + en_word[0] + "ay";
}


Please enter a sentence :
I say hello to all friends.
i
say
hello
to
all
friends

piglatin: 
iway aysay ellohay otay allway riendsfay



Welcome to the forum! And thank you for posting a clear description of the assignment and your problem.

First, you need to realize that programming is all about being precise. The assignment tells you precisely what three functions should look like, so you need to use them:
1
2
3
Word * splitSentence(const string words, int &size);
void convertToPigLatin(Word [] wordArr, int size);
void displayPigLatin(const Word [] wordArr, int size);

You need to use these exact declarations, not
1
2
void split_sent( string & sentSubs, vector<Word> & wordlist )
void en_word_to_piglatin( const string& en_word, string & piglatin_word )


When splitting the words, don't forget to remove punctuation.

i suggest that you start with a dummy version of convertToPigLatin() that simply copies the english word to the piglatin one:
1
2
3
4
5
6
void convertToPigLatin(Word [] wordArr, int size)
{
    for (int i=0; i<size; ++i) {
        wordArr[i].piglatin = wordArr[i].english;
    }
}


Now write splitSentence() and displayPigLatin(). Test what you have. Does it get the number of words right? Does it handle punctuation? Does it convert each letter to lower case?

When you're convinced that you're reading and displaying the words correctly, you can concentrate on convertToPigLatin(). The instructions give the exact algorithm to convert english to piglatin, you just have to follow it.

Topic archived. No new replies allowed.