Write a program that translates a text to Fake Latin and back. English is transl
ID: 3872195 • Letter: W
Question
Write a program that translates a text to Fake Latin and back. English is translated to Fake Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’ to each word. As an example, if your program reads from the keyboard the string “The quick brown fox” then it should print on the screen the following text: “Hetay uickqay rownbay oxfay”. You should also implement the reverse of this, reading "Iay ikelay rogrammingpay" should print on the screen "I like programming". i need it in java
Explanation / Answer
//
// FakeLatin.cpp
#include <string>
#include <sstream>
#include <iostream>
#include <cctype>
//Function converts sentence into FakeLatin
std::string toFakeLatin(const std::string& sentence){
std::stringstream is(sentence);
std::stringstream os;
std::string word;
while(is >> word){
//check if first letter is capital
bool iscapital = std::isupper(word[0]) != 0;
//adds 'ay' and also puts first letter in right position
std::string r = word.substr(1) + (char)std::tolower(word[0]) + "ay";
//capitalize if necessary
if(iscapital){
r[0] = (char)std::toupper(r[0]);
}
os << r << " ";
}
return os.str();
}
//Function converts FakeLatin into sentence
std::string fromFakeLatin(const std::string& sentence){
std::stringstream is(sentence);
std::stringstream os;
std::string word;
while(is >> word){
if(word.length() <= 2) continue;
//check if first letter is capital
bool iscapital = std::isupper(word[0]) != 0;
//remove 'ay' and also puts first letter in right position
std::string r = word[word.length()-3] +
word.substr(0, word.length()-3);
//capitalize if necessary
if(iscapital){
r[0] = (char)std::toupper(r[0]);
}
os << r << " ";
}
return os.str();
}
int main(){
std::cout << toFakeLatin("The quick brown fox") << std::endl;
std::cout << fromFakeLatin("Iay ikelay rogrammingpay") << std::endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.