0

I'm working on a fun random ICAO translator program and I'm almost done with it but I'm having one little problem. How do I go about splitting a string by each character? For example the output I want is; The word mike translated in the ICAO alphabet is:

m: Mike

i: Indiana

k: Kilo

e: Echo

So far I just get; The word mike translated in the ICAO alphabet is:

Mike

Indiana

Kilo

Echo

Apparently my post is mostly code and I must add more detail so I'm adding this sentence to hopefully satisfy the requirements. Also the translation should be right on top of each other and not one extra space down. I'm having problems with that and idk how to fix that.

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string word = " ", phonetic;
   int count = 0;
   
   cout << "Enter a word: ";
   cin >> word;
   
   while(count < word.length())
    {
        switch(word.at(count))
        {
            case 'A': case 'a': phonetic += " Alpha\n";
                break;
            case 'B': case 'b': phonetic += " Bravo\n";
                break;
            case 'C': case 'c': phonetic += " Charlie\n";
                break;
            case 'D': case 'd': phonetic += " Delta\n";
                break;
            case 'E': case 'e': phonetic += " Echo\n";
                break;
            case 'F': case 'f': phonetic += " Foxtrot\n";
                break;
            case 'G': case 'g': phonetic += " Golf\n";
                break;
            case 'H': case 'h': phonetic += " Hotel\n";
                break;
            case 'I': case 'i': phonetic += " Indiana\n";
                break;
            case 'J': case 'j': phonetic += " Juliet\n";
                break;
            case 'K': case 'k': phonetic += " Kilo\n";
                break;
            case 'L': case 'l': phonetic += " Lima\n";
                break;
            case 'M': case 'm': phonetic += " Mike\n";
                break;
            case 'N': case 'n': phonetic += " November\n";
                break;
            case 'O': case 'o': phonetic += " Oscar\n";
                break;
            case 'P': case 'p': phonetic += " Papa\n";
                break;
            case 'Q': case 'q': phonetic += " Quebec\n";
                break;
            case 'R': case 'r': phonetic += " Romeo\n";
                break;
            case 'S': case 's': phonetic += " Sierra\n";
                break;
            case 'T': case 't': phonetic += " Tango\n";
                break;
            case 'U': case 'u': phonetic += " Uniform\n";
                break;
            case 'V': case 'v': phonetic += " Victor\n";
                break;
            case 'W': case 'w': phonetic += " Whiskey\n";
                break;
            case 'X': case 'x': phonetic += " X-Ray\n";
                break;
            case 'Y': case 'y': phonetic += " Yankee\n";
                break;
            case 'Z': case 'z': phonetic += " Zulu\n";
                break;
            default: cout << "You did not enter a name" << endl;
            
        }
        count++;
    }

    cout << "The word "<< word <<" in the ICAO alphabet is:\n" 
    << phonetic << endl;
    
    return 0;
}
5
  • 2
    What does "split a string by character" means? After asking that, you then proceed and describe something that's completely different. The shown code already demonstrates the ability to process each character in a string, one character at a time. That seems to fit the definition of "split a string by character", so your question is unclear. Commented Apr 24, 2021 at 18:20
  • I didn't know the best way to word it. I tried to put an example of what I meant in the description above. Commented Apr 24, 2021 at 18:21
  • Ok, that's fine. So, the grand total is: the shown code already demonstrates that you know how to add strings together, one string at a time. That's how you form the phonetic string. So, just add another string, before each one of these, containing each character in question? Commented Apr 24, 2021 at 18:22
  • Can't you just do phonetic += word.at(count) before the switch? Commented Apr 24, 2021 at 18:23
  • Yes! phonetic +=word.at(count) before the switch is what I was looking for/missing. Thank you superStormer. I have now learned how to iterate a string a through it's characters. Commented Apr 25, 2021 at 16:25

3 Answers 3

2

To go through a string, you can simply use iterators:

std::string test_string = "test";
for( auto const& character : test_string )
{
    std::cout << character << "\n";
}

The whole program can be simplified, by using a map:

// Example program
#include <iostream>
#include <map>
#include <string>

char to_lower(char ch)
{
    return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
}

int main()
{
    const std::map<char, std::string> phonetic_alphabet = 
    {
         {'a', "Alpha"}
        ,{'b', "Bravo"}
        ,{'c', "Charlie"}
        ,{'d', "Delta"}
        ,{'e', "Echo"}
        ,{'f', "Foxtrot"}
        ,{'g', "Golf"}
        ,{'h', "Hotel"}
        ,{'i', "Indiana"}
        ,{'j', "Julia"}
        ,{'k', "Kilo"}
        ,{'l', "Lima"}
        ,{'m', "Mike"}
        ,{'n', "November"}
        ,{'o', "Oscar"}
        ,{'p', "Papa"}
        ,{'q', "Quebec"}
        ,{'r', "Romeo"}
        ,{'s', "Sierra"}
        ,{'t', "Tango"}
        ,{'u', "Uniform"}
        ,{'v', "Victor"}
        ,{'w', "Whiskey"}
        ,{'x', "X-Ray"}
        ,{'y', "Yankee"}
        ,{'z', "Zulu"}
    };
    
  std::cout << "Enter a word: ";
  std::string word;
  std::cin >> word;
  for( auto const& c : word )
  {
      const char lower_c = to_lower(c);
        if( phonetic_alphabet.find(lower_c) != phonetic_alphabet.end() )
        {
            std::cout << phonetic_alphabet.at(lower_c) << " ";
        }
        else
        {
            std::cout << c << " ";
        }
  }
}

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand your post correctly, you don't want to split a string but to iterate through its characters.

In C++11:

for (char& c : word) {
    // switch (c)
}

1 Comment

Thank you, that also worked but I went with the phonetic += word.at(count) before the switch
0

The answer of infinitezero is totally ok and probably the best solution in this case.

I would like to addtionally show a loop-less solution, also based on a std::unordered_map and able to read a complete sentence.

The basic idea is: Based on the requirement to transform single characters to ICAOwords, I decided to use a dedicated function for this purpose: std::transform. You may see the documentation here

Please see the additional / alternative solution below:

#include <iostream>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <iterator>

int main()
{
    // Preinitialize an unordered map
    std::unordered_map<int, std::string> alpha =
    {
        {'a', "Alpha"},{'b', "Bravo"},{'c', "Charlie"},{'d', "Delta"},{'e', "Echo"},{'f', "Foxtrot"},{'g', "Golf"},
        {'h', "Hotel"},{'i', "Indiana"},{'j', "Julia"},{'k', "Kilo"},{'l', "Lima"} ,{'m', "Mike"},{'n', "November"},
        {'o', "Oscar"},{'p', "Papa"},{'q', "Quebec"},{'r', "Romeo"},{'s', "Sierra"},{'t', "Tango"},{'u', "Uniform"},
        {'v', "Victor"},{'w', "Whiskey"},{'x', "X-Ray"},{'y', "Yankee"},{'z', "Zulu"}};

    // Read word form user
    if (std::string word; std::getline(std::cin, word)) {

        // Show output to user
        std::cout << "\n\n" << word << " --> ";

        // Convert and show
        std::transform(word.begin(), word.end(), std::ostream_iterator<std::string>(std::cout, " "),
            [&](const char c) { return (isalpha(c) ? alpha[tolower(c)] : ""); });
    }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.