As part of a project I'm working on, I need to develop a program that can take in a sentence and convert it somehow (I'm choosing to convert into Pig Latin). At first I thought this was going to be a piece of cake, until I realized that after

#include <iostream>

using namespace std;

int main(){
    
    string sentence;
    cout << "Please enter the phrase you want to be translated: ";
    getline(cin, sentence);

return 0;}

I have no real good idea of how to iterate through to the string so I can get it to do what I want. I need to separate each individual word (so copy every letter up to the first space), check whether the first letter of that word is a vowel or not (if vowel, do nothing), and if it's not a vowel move the first letter to the end of the word and add the letters 'ay', and then display it. An example would be

this is a phrase
histay is a hrasetay


Any help on how to accomplish such an easy sounding yet somehow ridiculously hard feat would be appreciated.

Recommended Answers

All 4 Replies

You might try the find_first_of( ) function to locate any word separators. Knowing where a word begins and ends, you can then use a substring function to extract it to a string that you will modify.

Member Avatar for jencas

Seach www.codeproject.com for 'Tokenizer'. There is a nice template based solution there.

hey Ellisande, you said you've spent a lot of time on this, so I think I can give you a sample program. Please notice that it's not a perfect solution, you need to refine it, considering many error conditions, etc. Also, if you have any questions about the function I use, please refer to the C++ Standard Library

#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;

bool isVowel(char c)
{
	return (c == 'a' || c == 'i' || c == 'u' || c == 'e' || c == 'o');
}

void toPigLatin(string& str)
{
	if (!str.empty() && !isVowel(str[0]))
	{
		str.push_back(str[0]);
		str += "ay";
		str.erase(0, 1);
	}
}

int main()
{
	// test phrase
	string phrase = "this is a phrase";

	stringstream ss(phrase);
	vector<string> v;

	copy(istream_iterator<string>(ss), 
		istream_iterator<string>(), 
		back_inserter(v));

	for_each(v.begin(), v.end(), toPigLatin);
	copy(v.begin(), v.end(), ostream_iterator<string>(cout, " "));
}

Wow, you've given me quite a bit to mull over. After doing quite a bit of googling and reading (gotta love cplusplus reference manual), I'm fairly sure I understand how this code works. The only problem is my IDE keeps throwing a compile error at istream_iterator citing a first use of this function problem, even though I'm pretty sure it's a part of iostream...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.