我正在做一个有趣的国际民航组织翻译程序,我几乎完成了,但我有一个小问题。如何按每个字符分割字符串?例如,我想要的输出是;在国际民航组织字母表中翻译的单词mike是:
m:迈克
i:印第安纳州
k:公斤
e:回声
到目前为止,我才知道,国际民航组织字母中的mike一词是:
麦克
印第安纳州
基洛
回声
显然,我的文章主要是代码,我必须增加更多细节,所以我添加这个句子希望满足需求。此外,翻译应该是在对方之上,而不是一个额外的空间下来。我对此有问题,我想知道如何解决这个问题。
#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;
}发布于 2021-04-24 18:21:58
如果我正确理解您的帖子,您不希望拆分字符串,而是迭代字符串中的字符。
在C++11中:
for (char& c : word) {
// switch (c)
}发布于 2021-04-24 18:33:02
要遍历字符串,只需使用迭代器:
std::string test_string = "test";
for( auto const& character : test_string )
{
std::cout << character << "\n";
}通过使用地图,可以简化整个程序:
// 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 << " ";
}
}
}发布于 2021-04-25 11:24:03
无穷小零的答案是完全可以的,在这种情况下可能是最好的解决方案。
我想附加显示一个无循环的解决方案,也是基于一个std::unordered_map,并能够阅读完整的句子。
基本思想是:基于将单个字符转换为ICAOwords的要求,我决定为此使用一个专用函数:std::transform。您可能会看到文档这里
请参阅下文的补充/备选解决方案:
#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;
}https://stackoverflow.com/questions/67246056
复制相似问题