I have a challenge:
In Sweden, there is a simple child's game similar to Pig Latin called Rövarspråket (Robbers Language).
In the CCC version of Rövarspråket, every consonant is replaced by three letters, in the following order:
- the consonant itself;
- the vowel closest to the consonant in the alphabet (e.g. d → e), with the rule that if the consonant falls exactly between two vowels, then the vowel closer to the start of the alphabet will be chosen (e.g. c → a);
- the next consonant in the alphabet following the original consonant (e.g. d → f) except if the original consonant is z, in which case the next consonant is z as well.
Vowels (a, e, i, o, u) in the word remain the same.
Write a program that translates a word from English (one line of no more than 30 lower-case letters) into Rövarspråket. Example:
"joy"
→"jikoyuz"
.
Here is my working solution:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message, newmessage="";
cin>>message;
for(int i=0; i<message.length(); ++i){
if(message[i]=='a'||message[i]=='e'||message[i]=='i'||message[i]=='o'||message[i]=='u'){
newmessage+=message[i];
}
else if(message[i]=='b'||message[i]=='c'||message[i]=='d'||message[i]=='f'||message[i]=='g'||
message[i]=='h'||message[i]=='j'||message[i]=='k'||message[i]=='l'||message[i]=='m'||
message[i]=='n'||message[i]=='p'||message[i]=='q'||message[i]=='r'||message[i]=='s'||
message[i]=='t'||message[i]=='u'||message[i]=='v'||message[i]=='w'||message[i]=='x'||
message[i]=='y'||message[i]=='z'){
//the letter itself
newmessage+=message[i];
//the vowel closest to the consonant
if(message[i]=='b'){newmessage+="a";}
else if(message[i]=='c'){newmessage+="a";}
else if(message[i]=='d'){newmessage+="e";}
else if(message[i]=='f'){newmessage+="e";}
else if(message[i]=='g'){newmessage+="e";}
else if(message[i]=='h'){newmessage+="i";}
else if(message[i]=='j'){newmessage+="i";}
else if(message[i]=='k'){newmessage+="i";}
else if(message[i]=='l'){newmessage+="i";}
else if(message[i]=='m'){newmessage+="o";}
else if(message[i]=='n'){newmessage+="o";}
else if(message[i]=='p'){newmessage+="o";}
else if(message[i]=='q'){newmessage+="o";}
else if(message[i]=='r'){newmessage+="o";}
else if(message[i]=='s'){newmessage+="u";}
else if(message[i]=='t'){newmessage+="u";}
else if(message[i]=='v'){newmessage+="u";}
else if(message[i]=='w'){newmessage+="u";}
else if(message[i]=='x'){newmessage+="u";}
else if(message[i]=='y'){newmessage+="u";}
else if(message[i]=='z'){newmessage+="u";}
//the next consonant in the alphabet
if(message[i]=='b'){newmessage+="c";}
else if(message[i]=='c'){newmessage+="d";}
else if(message[i]=='d'){newmessage+="f";}
else if(message[i]=='f'){newmessage+="g";}
else if(message[i]=='g'){newmessage+="h";}
else if(message[i]=='h'){newmessage+="j";}
else if(message[i]=='j'){newmessage+="k";}
else if(message[i]=='k'){newmessage+="l";}
else if(message[i]=='l'){newmessage+="m";}
else if(message[i]=='m'){newmessage+="n";}
else if(message[i]=='n'){newmessage+="p";}
else if(message[i]=='p'){newmessage+="q";}
else if(message[i]=='q'){newmessage+="r";}
else if(message[i]=='r'){newmessage+="s";}
else if(message[i]=='s'){newmessage+="t";}
else if(message[i]=='t'){newmessage+="v";}
else if(message[i]=='v'){newmessage+="w";}
else if(message[i]=='w'){newmessage+="x";}
else if(message[i]=='x'){newmessage+="y";}
else if(message[i]=='y'){newmessage+="z";}
else if(message[i]=='z'){newmessage+="z";}
}
}
cout<<newmessage;
return 0;
}
I would like to avoid hardcoding.
Is there any way to avoid using all those if else statements?
std::string lhs("bcdfghjklmnpqrstvwxyz"), rhs("aeeeiiiiooooouuuuuuu"); auto it = lhs.find(message[i]); if (it != lhs.end()) newmessage += rhs[it - lhs.begin()];
. \$\endgroup\$