Reputation: 155
I am a complete c++ beginner and this knowledge sort of comes from other languages I tried to learn. The code below is a function from a morse code translator that I am trying to build and I am pretty sure that this is not even close to "a good way" of doing it. My question is, how would I make the program look through the the string that the user typed in and change each occurrence of the letter to morse.
string ReplaceAll(std::string str, const std::string& from, const std::string& to){
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;}
void Translate(string s) {
static string s2 = ReplaceAll(string(s), std::string("a"), std::string(".- "));
static string s3 = ReplaceAll(string(s2), std::string("b"), std::string("-... "));
static string s4 = ReplaceAll(string(s3), std::string("c"), std::string("-.-. "));
static string s5 = ReplaceAll(string(s4), std::string("d"), std::string("-.. "));
static string s6 = ReplaceAll(string(s5), std::string("e"), std::string(". "));
static string s7 = ReplaceAll(string(s6), std::string("f"), std::string("..-. "));
static string s8 = ReplaceAll(string(s7), std::string("g"), std::string("--. "));
static string s9 = ReplaceAll(string(s8), std::string("h"), std::string(".... "));
static string s10 = ReplaceAll(string(s9), std::string("i"), std::string(".. "));
static string s11 = ReplaceAll(string(s10), std::string("j"), std::string(".--- "));
static string s12 = ReplaceAll(string(s11), std::string("k"), std::string("-.- "));
static string s13 = ReplaceAll(string(s12), std::string("l"), std::string(".-.. "));
static string s14 = ReplaceAll(string(s13), std::string("m"), std::string("-- "));
static string s15 = ReplaceAll(string(s14), std::string("n"), std::string("-. "));
static string s16 = ReplaceAll(string(s15), std::string("o"), std::string("--- "));
static string s17 = ReplaceAll(string(s16), std::string("p"), std::string(".--. "));
static string s18 = ReplaceAll(string(s17), std::string("q"), std::string("--.- "));
static string s19 = ReplaceAll(string(s18), std::string("r"), std::string(".-. "));
static string s20 = ReplaceAll(string(s19), std::string("s"), std::string("... "));
static string s21 = ReplaceAll(string(s20), std::string("t"), std::string("- "));
static string s22 = ReplaceAll(string(s21), std::string("u"), std::string("..- "));
static string s23 = ReplaceAll(string(s22), std::string("v"), std::string("...- "));
static string s24 = ReplaceAll(string(s23), std::string("w"), std::string(".-- "));
static string s25 = ReplaceAll(string(s24), std::string("x"), std::string("-..- "));
static string s26 = ReplaceAll(string(s25), std::string("y"), std::string("-.-- "));
static string s27 = ReplaceAll(string(s26), std::string("z"), std::string("--.. "));
cout << s27 << endl;
}
Upvotes: 2
Views: 129
Reputation: 3212
It is a better solution to build a std::map
with the replacements and go through all the characters and build a new string. With your solution characters like .
, -
, !
are in the result.
#include <iostream>
#include <map>
typedef std::map<char, const char*> Replacements;
std::string Translate(const Replacements& r, std::string s)
{
std::string result;
result.reserve(s.size() * 5); // optional: reserve guessed number of elements for new string
// for every element of the string
for (char c : s)
{
// search for replacement
Replacements::const_iterator iter = r.find(c);
if (iter != r.end())
{
// found replacement
result += iter->second;
result.push_back(' ');
}
}
return result;
}
int main()
{
Replacements morse_code;
morse_code['a'] = ".-";
morse_code['b'] = "-...";
morse_code['c'] = "-.-.";
// ...
std::string in;
if (std::cin >> in)
std::cout << Translate(morse_code, in) << '\n';
}
Upvotes: 2
Reputation: 118330
This is a wrong approach. Instead of trying to replace the contents of the string, it's much easier to simply create a new string:
std::string TranslateAll(const std::string &s)
{
std::ostringstream o;
for (char c:s)
o << Translate(c);
return o.str();
}
Now, you can write a much simpler
const char *Translate(char c)
All that it does is take a single character as a parameter, and returns its morse code, as a simple character string.
Much easier.
Upvotes: 0