There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such as parents) cannot understand. The rules are:
If a word begins with a vowel (aeiou) then append "way" to the word. For example, "Aggie" becomes "Aggieway".
Otherwise, remove the consecutive non-vowels from the beginning of the word, append them to the end of the word, and append "ay" to the word. For example, "whoop" becomes "oopwhay".
Write a program named hw5pr1.cpp which reads words from a file named in.txt and writes the words in Pig Latin to a file named out.txt.
So if in.txt contains the words
I love Reveille
This is the street
then the words
Iway ovelay eveilleRay
isThay isway ethay eetstray
will be written to out.txt.
#include <iostream>
#include <string>
#include <fstream>
#include <locale> //for ispunct
#include <sstream> //for stringstream
#include <cctype> //for toupper, tolower
using namespace std;
//prototypes
bool firstIsVowel(char first, const string VOWEL){
for (size_t j = 0; j < (VOWEL.length()-1);
j++){
//cout<<"Length is:
"<<VOWEL.length() <<endl;
//cout<<"J is: "
<<j<<endl;
if(first == VOWEL[j]){
return true;
}
}
return false;
}
int main(){
//vars
ifstream inFile;
ofstream outFile;
string filename;
string inLine;
//string outline;
string PUNC = "\"!\'";
string UPPER;
const string VOWEL = "aeiouAEIOU";
//loop for in and out files
do{
cout<<"Enter input file name:
";
cin >> filename;
inFile.open(filename);
}while (inFile.fail());
do{
cout<<"Enter output file
name: ";
cin >> filename;
outFile.open(filename);
}while (outFile.fail());
string a = " ";
while(!inFile.eof()){
//readline
getline(inFile,inLine); //sets char
delim to ' '
string inWord;
stringstream ss(inLine); //makes
the inLine into a stream so we can read individual strings from
it
//readword from read line
while (getline(ss, inWord, ' ')){
//sets char delim to ' '
size_t i =
0;
//translate to
pig latin
string endPig =
"ay";
cout<<"Current word is:"<<inWord<<endl;
while
(ispunct(inWord[i])){
cout<<"Current char is punct and
is"<<inWord[i]<<endl;
i++;
}
//if (inWord[i]
is vowel) //ex: test_(lower.find_first_of(UPPER) == string::npos);
//but change to if vowels
if
(firstIsVowel(inWord[i], VOWEL)){
endPig = "way";
}
//if (inWord[i]
is consonant) //ex: test_(lower.find_first_of(UPPER) ==
string::npos); //but change to if !vowels
else{
//inWord.erase(i);
if (isupper(inWord[i])){
cout <<"Char is caps
and is: " <<inWord[i]<<endl;
cout<<"Second Char is
lower and is: "<< inWord[i+1]<<endl;
inWord[i+1]=toupper(inWord[i+1]);
inWord[i]=tolower(inWord[i]);
cout <<"Second char is
now upper too is: "<< inWord[i+1]<<endl;
}
endPig = inWord.at(i) + endPig;
cout << "End pig is:" <<
endPig<<endl;
if (i == 0){
inWord.erase(i, i);
}
inWord.erase(i, 1);
}//end consonant
else
//check for punc
at end, append pigEnd
int k = 1;
while
(ispunct(inWord[inWord.length()-k])){ //change to for loop that
checks length minus j while j == punct
cout<<"Last char is punct and is: "
<<inWord[inWord.length()-k]<<endl;
k++;
}
k--;
//inWord =
inWord.insert((inWord.length()-k), endPig);//inserts endPig before
end punc
if
(k>=1){
inWord =
inWord.insert((inWord.length() -k), endPig);//inserts endPig before
end punc
cout<<"Pig
inserted before end punct char: "<< inWord<<endl;
}
else{
inWord = inWord.insert((inWord.length()),
endPig);
}
//write
line
cout<<"Writing inWord to outfile:
"<<inWord<<endl;
outFile <<
inWord<<" ";
}
outFile << "\n";
}
}
in.txt
I love Reveille
This is the street
then the words
There is a children's "secret language" called "Pig Latin" which supposedly allows conversation the uninformed (such...
**IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...