473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ Translate English Sentence to Pig Latin

13
I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so that each word is translated to Pig latin seperately. Attached is my code: Thank you for your time.

the out put for hello world comes out :ello worldhay
I want it to say: ellohay orldway

// testpig.cpp : Defines the entry point for the console application.
//

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



bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main ()
{

const int LENGTH = 100;
char str [LENGTH];



string word, result;

cout << "Enter a string or, blank to exit." <<endl;
cout << "The program will output the words in Pig Latin."<<endl;
cin.getline(str,LENGTH);
cout<<endl;

cout << "The pig Latin form of " << str <<" is:\n"<< pigLatinString(str) << endl;

return 0;
/* Process the input line word by word */



};









bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E':
case 'I': case 'O':
case 'U':
case 'a': case 'e':
case 'i': case 'o':
case 'u':
return true;
default: return false;
};
}//end isVowel

string rotate(string pStr)

{
string::size_type len=pStr.length();

string rStr;

rStr=pStr.substr(1,len-1)+pStr[0];////

return rStr;
}

string pigLatinString(string pStr)

{
string::size_type len;

bool foundVowel;

string::size_type counter;

if (isVowel(pStr[0]))

pStr=pStr+"way";

else
{//didn't start with a vowel

pStr = pStr + "";
pStr=rotate(pStr);
len=pStr.length();
foundVowel=false;

for (counter = 1; counter<len-1; counter++)

if (isVowel(pStr[0]))

{
foundVowel=true;
break;
}

else

pStr=rotate(pStr);

if (!foundVowel)

pStr=pStr.substr(1,len)+"way";

else

pStr = pStr + "ay";
}
return pStr;

}
Nov 9 '06 #1
10 22777
sicarie
4,677 Expert Mod 4TB
I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so that each word is translated to Pig latin seperately. Attached is my code: Thank you for your time.
Check out the C++ function strtok(). I think you can use that to separate out the words, as long as you know the delimiters that will be separating them (like spaces).
Nov 9 '06 #2
manontheedge
175 100+
you can read it in as characters, and signal the end of a word every time it sees a space...that's one way to do it...by avoid strings entirely.
Nov 9 '06 #3
ahoway
13
do either of you know where I get a tutorial on this? My book is not very clear and none of my professors example programs show this. Thanks
Nov 9 '06 #4
sicarie
4,677 Expert Mod 4TB
do either of you know where I get a tutorial on this? My book is not very clear and none of my professors example programs show this. Thanks
Strtok:
http://www.cplusplus.com/ref/cstring/strtok.html

For manontheedge's method, it's less of something there would be a tutorial on, you would create a loop that accepted a char, and test it against the value for a 'space', and then did whatever else you wanted.
Nov 9 '06 #5
Strtok:
http://www.cplusplus.com/ref/cstring/strtok.html

For manontheedge's method, it's less of something there would be a tutorial on, you would create a loop that accepted a char, and test it against the value for a 'space', and then did whatever else you wanted.
Read the whole string the way u have done in ur program.
then extract out each word till u find a space
copy the word to another string from subscript 1 , and concatenate 0th subscript and "way"
continue dor the whole length of the string
Nov 9 '06 #6
ahoway
13
I have solved the first problem, now I need help with...

1.Program repeats until user enters a null string
2.Prompts the user to enter a string or just return to terminate

here is my new code: Thank you in advance!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;



bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main ()
{

string word,line;

cout << "Enter a string or, blank to exit." <<endl;
cout << "The program will output the words in Pig Latin."<<endl;
getline(cin, line);


cout << "The pig Latin form of " << line <<" is:\n";




/* Process the input line word by word */


while ( line.length() != 0)
{
//cout << "==> Enter line of text or enter return to stop input\n";




int lineLen = line.length();
nt wordLen = line.find(' ');
if (wordLen > 0)
{
word = line.substr(0,wordLen);
line = line.substr(wordLen+1);

cout << pigLatinString(word) << " ";
}
else
{
cout << pigLatinString(line) <<"\n ";

line = "";
}
}


return 0;
};



////////////////////////////////////////////////////////these are words starting with a vowel
bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E': case 'I': case 'O':case 'U':
case 'a': case 'e':case 'i': case 'o': case 'u':
return true;
default: return false;
};
}///////////////////////////////////////////////////////////////end isVowel

string rotate(string pStr)

{
string::size_type len=pStr.length();

string rStr;

rStr=pStr.substr(1,len-1)+pStr[0];

return rStr;
}

string pigLatinString(string pStr)

{
string::size_type len;

bool foundVowel;

string::size_type counter;

if (isVowel(pStr[0]))

pStr=pStr+"way";

else
{//These are words that didn't start with a vowel



len=pStr.length();
foundVowel=false;



if (isVowel(pStr[0]))

{
foundVowel=true;
}

else

pStr=rotate(pStr);

if (!foundVowel)

pStr=pStr.substr(0,len)+"way";

else

pStr = pStr + "ay";
}
return pStr;

}
Nov 10 '06 #7
sicarie
4,677 Expert Mod 4TB
I have solved the first problem, now I need help with...

1.Program repeats until user enters a null string
2.Prompts the user to enter a string or just return to terminate

here is my new code: Thank you in advance!
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;



bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main ()
{

string word,line;

cout << "Enter a string or, blank to exit." <<endl;
cout << "The program will output the words in Pig Latin."<<endl;
getline(cin, line);


cout << "The pig Latin form of " << line <<" is:\n";




/* Process the input line word by word */


while ( line.length() != 0)
{
//cout << "==> Enter line of text or enter return to stop input\n";




int lineLen = line.length();
nt wordLen = line.find(' ');
if (wordLen > 0)
{
word = line.substr(0,wordLen);
line = line.substr(wordLen+1);

cout << pigLatinString(word) << " ";
}
else
{
cout << pigLatinString(line) <<"\n ";

line = "";
}
}


return 0;
};



////////////////////////////////////////////////////////these are words starting with a vowel
bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E': case 'I': case 'O':case 'U':
case 'a': case 'e':case 'i': case 'o': case 'u':
return true;
default: return false;
};
}///////////////////////////////////////////////////////////////end isVowel

string rotate(string pStr)

{
string::size_type len=pStr.length();

string rStr;

rStr=pStr.substr(1,len-1)+pStr[0];

return rStr;
}

string pigLatinString(string pStr)

{
string::size_type len;

bool foundVowel;

string::size_type counter;

if (isVowel(pStr[0]))

pStr=pStr+"way";

else
{//These are words that didn't start with a vowel



len=pStr.length();
foundVowel=false;



if (isVowel(pStr[0]))

{
foundVowel=true;
}

else

pStr=rotate(pStr);

if (!foundVowel)

pStr=pStr.substr(0,len)+"way";

else

pStr = pStr + "ay";
}
return pStr;

}
I think your best bet is a while loop.
Nov 10 '06 #8
ahoway
13
Now I can get the loop to work, but my English to pig latin is gone?
// testpig.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;



bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main ()
{

string word,line;

cout << "Enter a string or, blank to exit." <<endl;
cout << "The program will output the words in Pig Latin."<<endl;
getline(cin, line);
cout<<endl;
cout << "The pig Latin form of " << line <<" is:\n";

while (line.length() !=0)
{

cout << "Enter a string or, blank to exit." <<endl;
cout << "The program will output the words in Pig Latin."<<endl;
getline(cin, line);
}


/* Process the input line word by word */


while ( line.length() != 0)
{
//cout << "==> Enter line of text or enter return to stop input\n";




int lineLen = line.length();
int wordLen = line.find(' ');

if (wordLen > 0)
{
word = line.substr(0,wordLen);
line = line.substr(wordLen+1);

cout << pigLatinString(word) << " ";
}
else
{
cout << pigLatinString(line) <<"\n ";

line = "";
}
}


return 0;
};



////////////////////////////////////////////////////////these are words starting with a vowel
bool isVowel(char ch)
{
switch (ch)
{
case 'A': case 'E': case 'I': case 'O':case 'U':
case 'a': case 'e':case 'i': case 'o': case 'u':
return true;
default: return false;
};
}///////////////////////////////////////////////////////////////end isVowel

string rotate(string pStr)

{
string::size_type len=pStr.length();

string rStr;

rStr=pStr.substr(1,len-1)+pStr[0];

return rStr;
}

string pigLatinString(string pStr)

{
string::size_type len;

bool foundVowel;

string::size_type counter;

if (isVowel(pStr[0]))

pStr=pStr+"way";

else
{//These are words that didn't start with a vowel



len=pStr.length();
foundVowel=false;



if (isVowel(pStr[0]))

{
foundVowel=true;
}

else

pStr=rotate(pStr);

if (!foundVowel)

pStr=pStr.substr(0,len)+"way";

else

pStr = pStr + "ay";
}
return pStr;

}
Nov 10 '06 #9
sicarie
4,677 Expert Mod 4TB
Yeah, I'd use the while loop to test the input, and repeat output, then go on to the program, not do the program in a while loop...`
Nov 10 '06 #10
ahoway
13
Thanks for all the help. I changed my program around and this is what I came up with. It does exactly what the standards called for.

// piglatin_2.cpp : Defines the entry point for the console application.
//
// PigLatin.cpp : Defines the entry point for the console application.
//This program will take in a string of characters from a sentence. It will
//then look at each word and convert the word to Pig Latin. It will then
//put the sentence back toghther in Pig LAtin form. It has a loop inside it
// to continuously test for new strings in which the user types in. If there
//is nothing to be typed in and the NULL condition is satisfied the program will then
//break out of the loop..Does not look for punctuation

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


///////////////////////////////////////////////////////////////////////
///////////////////////FUNCTION LIST///////////////////////////////////////////////
bool isVowels(char x);
void Vowel(string changed);
void nonVowel(char begin, string end);
void changeLine (string line);
void printHeader();

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////BEGIN THE MAIN PROGRAM///////////////////////////////////////////


int main()
{

char ch;
string word, first, line;

printHeader();//prints my information
cout <<endl;
cout << "Enter a string or, blank to exit.\n";
cout << "This program will output the words in Pig Latin.\n";
cout <<endl;
getline(cin,line);//input a line of words
cout <<endl;
while (line.length()!= 0)//this loop was formed to keep the user able to
//input more sentences to be reconfigured into Pig Latin. If the loop recognizes
//the null space it will break out of the loop and end the program.
{
changeLine(line);// output of the new sentence in Pig Latin
cout<<endl;
cout<<endl;
cout << "Enter a string or, blank to exit.\n";
cout << "This program will output the words in Pig Latin.\n\n";
cout <<endl;
getline(cin,line);//input a line of words
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////END OF MAIN//////////////////////////////////////////


bool isVowels(char x) // this is where all of the vowels in the words are found. It will
//read in the word, change the letter to loower case.
{
char low_case = tolower (x);
if (low_case == 'a' )
return true;
else if (low_case == 'e' )
return true;
else if ( low_case== 'i' )
return true;
else if (low_case == 'o' )
return true;
else if (low_case == 'u' )
return true;
else
return false;

}

void changeLine (string line)//this is where the strings are changed. As long as there is
//not a Null character, changeling will look at each word. It will look for the blank spaces
//between the words. One a word is found it is the look at to whether it has a vowel in it or not
// then it is rearranged into a string.
{
string word, first;
char ch;
while (line.length() !=0)//line consist of characters
{
int wordLen = line.find(' ');
if (wordLen >0)
{
word = line.substr(0,wordLen);
first=word.substr(0,1);
ch = first[0];
string notFirst(word,1);
if (isVowels (ch))// vowel found
{
Vowel(word);
}
else
{
nonVowel(ch,notFirst);//no vowel found
}
line = line.substr(wordLen+1);
}
else
{
word = line;
ch = line[0];
string notFirst(word,1);
if (isVowels (ch))
Vowel(word);
else
nonVowel(ch, notFirst);
line="";
}
}
}
void Vowel(string changed)//what this does is when the string of the new word is brought in
//it is changed due to Pig latin configuration for words that begin with a vowel and then added "way" to the end of the word.
//this new word is then processed out to the string to put back to sentence form.
{
string newWord;
newWord=changed+"way";
cout<<newWord<<" ";
}

void nonVowel(char begin, string end)//what this does is when the string of the new word is brought in
//it is changed due to Pig latin configuration for words that do not begin
//with a vowel and then added "ay" to the end of the word.this new word is then processed
//out to the string to put back to sentence form.
{
string newWord;
newWord=end+begin+"ay";
cout<<newWord<<" ";
}
void printHeader()//This is the format for my header to print name and class info.
{
cout <<"\Anthony Howay"<<endl;
cout <<"csc 275 Prog 4-Pig Latin" <<endl;
cout <<"09 Nov. 2006"<<endl;

};
Nov 12 '06 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ricky Romaya | last post by:
Hi, Anybody could show me a regex for capturing words (alphas, without numerics) in languages other than english (languages with special characters i.e. french, german)? I've tried '+' but the...
3
by: F. GEIGER | last post by:
When I start a py2exe-ed application I get the error 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128) This is how I run py2exe: setup.py py2exe -O1...
8
by: Stanley Sinclair | last post by:
You wrote the pleural of "schema" as "schemata." My heart is warmed. Americans write that as "schemas," since they have no concept of the history of either their language nor their own...
66
by: jacob navia | last post by:
The english word "Initialized" exists. (Cambridge dictionary finds it). The word "Uninitialized" doesn't seem to exist, and no dictionary has it. I am using that word very often in my tutorial of...
4
by: Asaf | last post by:
Hi, For a particular system I need to transfer Hebrew text with encoding ISO-8859-8 and to flip only the English characters. For example the text: טקסט עברית With English ועוד...
3
by: dalearyous | last post by:
ok basically i need to write a program that will replace normal words in a sentence with pirate words. the trick is it needs to be able to take two word phrases. i went about this two different ways:...
12
by: Steve Howell | last post by:
The never-ending debate about PEP 3131 got me thinking about natural languages with respect to Python, and I have a bunch of mostly simple observations (some factual, some anecdotal). I present...
6
by: Ole Nielsby | last post by:
Does C++ have a method of retrieving this? When launching my app, I want to select the appropriate language for ts GUI, based on the user's language setting. The frameworks wxWidgets and...
1
by: per9000 | last post by:
We have a big application written without thought of supporting multiple languages with lots of strings in a default "english". Now the client wants to support any language (with a latin alphabet)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.