How do I use a switch statement with a string?

So, One of my lab assignments is to take a word and output how that word would be spelled using International Civil Aviation Organization Alphabet words. Now, I think I have somewhat of an idea as to how to go about doing this:
First, break the string up into 1-character substrings
Second, compare each substring against a letter. Now, I'm not sure how to do this part, because these are strings and switch statements need "char"s or "int"s.

Can someone please help? Thanks.
After going back and reading Chapter 4-1, it dawned on me that I was going about this whole problem wrong.
Do you still need help?

I would start with an array of strings. Each string represents the spelling of one letter - so you have 26 strings.

1
2
3
const unsigned int num_strings = 26;

std::string nato[num_strings] = {"Alfa", "Bravo", "Charlie" /*...*/};


Then, iterate through each letter in your word. Print out the string in the n'th location of the string array (where 'n' is the position that matches the current letter).

1
2
3
4
5
6
7
std::string word = "abc";

for (int i = 0; i < word.size(); ++i) {

	std::cout << nato[word[i] - 'a'] << std::endl;

}


This code assumes that the word has no numbers, uppercase letters or other special characters.
Last edited on
Topic archived. No new replies allowed.