translate a phrase to Pig Latin

I've already completed translating a word into pig latin, but now I need suggestions as to how I should go about converting the code to translate a whole sentence into pig latin. Here's what I have so far.
// PigLatinPhrase.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

const int stringLength = 50;

int countLetters(char ph[stringLength]);
void moveLetter(char ph[stringLength]);
bool notConjunction(char ph[stringLength]);
void translateToPigLatin(char ph[stringLength]);


int main()
{
char word[stringLength];
char answer;

do //Loop to repeat program
{
do //Loop if they entered nothing or a number so that it can restart
{
cout << "Please enter a word so that it can be translated into Pig Latin: ";
cin >> word;

if (isalpha(word[0]) && word != " ")//condition to make sure user entered a valid word
{
translateToPigLatin(word);
break;
}
else
cout << "Either you entered nothing or " << "your word starts with a number. " << endl;
cout << " Try again? enter y for yes: ";
cin >> answer;

} while (answer == 'Y' || answer == 'y');


cout << endl;
cout << "Would you like to do another word? Use 'Y' for yes. ";
cin >> answer;

} while (answer == 'Y' || answer == 'y');


return 0;
}


int countLetters(char ph[stringLength]) //counts number of letters in word
{
int i;
int count = 0;

// counting just the characters in a phrase not including spaces
for (i = 0; ph[i] != '\0'; i++)
{
if ((ph[i] >= 'a' && ph[i] <= 'z') || (ph[i] >= 'A' && ph[i] <= 'Z'))
count++;
}
return count;
}


void moveLetter(char ph[stringLength])
{
char temp = ph[0];//stores the first letter in another string
int i;

for (i = 0; i < stringLength - 1 && ph[i + 1] != 0; i++)
{
ph[i] = ph[i + 1];//shifts letters to the left of its original position in the string
}
ph[i] = temp;//adds the original
}

void countWords(char ph[500])
{
int i;
int count = 0;
bool inword = false;

for (i = 0; i < strlen(ph); i++)
{
if ((ph[i] >= 'a' && ph[i] <= 'z') || (ph[i] >= 'A' && ph[i] <= 'Z') || ph[i] == '\'')
{
if (!inword)
{
inword = true;
}

}
else
{
inword = false;
}
}
}



bool notConjunction(char ph[stringLength])//Takes care of the conjunction condition
{
if (!strcmp(ph, "and"))
{
return true;
}
if (!strcmp(ph, "but"))
{
return true;
}
if (!strcmp(ph, "for"))
{
return true;
}
if (!strcmp(ph, "nor"))
{
return true;
}
if (!strcmp(ph, "yet"))
{
return true;
}
else
return false;
}

void translateToPigLatin(char ph[stringLength])//Pig latin translator
{
char pigAddOn[] = "ay";//array to add-on the end of words that fit the conditions

if (notConjunction(ph) == false)//calls the conjunction function
{
if (countLetters(ph) > 2 && strcmp(ph, "the")) /* takes care of article parameters and
any words */
{
if ((ph[0] != 'a' && ph[0] != 'A') && (ph[0] != 'e' && ph[0] != 'E')
&& (ph[0] != 'i' && ph[0] != 'I') && (ph[0] != 'o' && ph[0] != 'O')
&& (ph[0] != 'u' && ph[0] != 'U'))/* Tests whether first character is a vowel or not.
I'm sure there's a more efficient and effective way
of doing this, but I didn't want to mess with it. */
{
moveLetter(ph);
strcat(ph, pigAddOn);
cout << "Your word in Pig Latin: " << ph << endl;
}
else
{
strcat(ph, pigAddOn);
cout << "Your word in Pig Latin: " << ph << endl;
}
}
else
cout << "Your word in Pig Latin is " << ph << endl;
}
else
cout << "Your word in Pig Latin is " << ph << endl;
}
Last edited on
Please edit your post and put [code] and [/code] around your code so that it is formatted.

Are you allowed to use std::string?

e.g.
1
2
3
4
5
6
7
8
9
10
11
// Example program
#include <iostream>
#include <string>

int main()
{
  std::string name;
  std::cout << "What is your name? ";
  getline (std::cin, name);
  std::cout << "Hello, " << name << "!\n";
}


_______________________________________________


http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/cstring/strtok/

I'm just going to go ahead and assume that you're not allowed to use std::strings, otherwise this would be a hell of a lot easier.

What you need to do it input a whole sentence at once, and then break the sentence into each word, and then call the functions you have on each word.

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
// Example program
#include <iostream>
#include <cstring>

void convert_to_pig_latin(char word[])
{
    // convert the individual word to pig latin,
    // like what you've already been doing
    std::cout << "Converting \"" << word << "\" to pig latin..." << std::endl;
}

int main()
{
    const int MAX_SENTENCE_SIZE = 1024;
    
    char sentence[MAX_SENTENCE_SIZE];
    
    std::cout << "Enter sentence: ";
    std::cin.getline(sentence, MAX_SENTENCE_SIZE); // get the sentence
    
    std::cout << "Splitting the sentence into words..." << std::endl;
    
    char* p_word = std::strtok(sentence, " ,.");
    
    // Process each word at a time:
    while (p_word != nullptr)
    {
        convert_to_pig_latin(p_word);
        p_word = std::strtok(nullptr, " ,.");
        
    }
}


Enter sentence: The hungry, hungry caterpillar ate everyone's food.
Splitting the sentence into words...
Converting "The" to pig latin...
Converting "hungry" to pig latin...
Converting "hungry" to pig latin...
Converting "caterpillar" to pig latin...
Converting "ate" to pig latin...
Converting "everyone's" to pig latin...
Converting "food" to pig latin...
Last edited on
Topic archived. No new replies allowed.