{
"question_id": 7446099, "question_title": "找出string中只出现过一次的字符", "question_content": "找出string中只出现过一次的字符\n例如\"abcdef abcd\"中需要得到ef", "difficulty": "简单", "answer_id": 53425698, "answer_content": "<p>#include <iostream><br />\n#include <string></p>\n\n<p>using namespace std;</p>\n\n<p>int main()<br />\n{<!-- --><br />\n string temp = "";<br />\n cout << "请输入字符串:";<br />\n cin >> temp;<br />\n string str = ""; //存放只出现一次的字符<br />\n string str1 = ""; //存放重复的字符<br />\n for (int i = 0; i < temp.length(); i++)<br />\n {<!-- --><br />\n string tempSub = temp.substr(i, 1); <br />\n int b = temp.rfind(tempSub); //从后向前查找字符出现的位置<br />\n if (i == b && str1.find(tempSub) == -1) //如果b和遍历查找的位置一致,且str1 里没有该字符,说明只出现一次<br />\n str +=temp.substr(i, 1);<br />\n else if (str1.find(tempSub) == -1)<br />\n str1 += temp.substr(i, 1);;<br />\n }<br />\n cout << "只出现一次的字符:" << str << endl;<br />\n cout << "重复出现的字符的字符:" << str1 << endl;<br />\n system("pause");<br />\n return 0;<br />\n}</p>\n\n<p style=\"text-align:center\"><img alt=\"\" src=\"https://img-ask.csdnimg.cn/upload/1623295486577.png\" /></p>\n\n<p> </p>\n", "tag_name": "c++", "cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint main()\n{\n string temp = \"\";\n cout << \"请输入字符串:\";\n cin >> temp;\n string str = \"\"; \n string str1 = \"\"; \n for (int i = 0; i < temp.length(); i++)\n {\n string tempSub = temp.substr(i, 1); \n int b = temp.rfind(tempSub); \n if (i == b && str1.find(tempSub) == -1) \n str +=temp.substr(i, 1);\n else if (str1.find(tempSub) == -1)\n str1 += temp.substr(i, 1);\n }\n cout << \"只出现一次的字符:\" << str << endl;\n cout << \"重复出现的字符的字符:\" << str1 << endl;\n system(\"pause\");\n return 0;\n}", "topic_link": "https://bbs.csdn.net/topics/600470278", "status": 1, "keywords": "算法,字符串", "license": "csdn.net", "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/129.ipynb?type=file" }, "notebook_enable": 1, "author": "Blackhere"
}