C++ 确定两个字符串是否为字谜
编写一个接受c字符串和字符作为参数的函数。返回字符在字符串中出现的次数 我也必须在我的计划中这样做: 将字符串A和B转换为所有小写。对于字符串A中的每个字母,调用A和B上的函数 这是我目前掌握的代码:C++ 确定两个字符串是否为字谜,c++,C++,编写一个接受c字符串和字符作为参数的函数。返回字符在字符串中出现的次数 我也必须在我的计划中这样做: 将字符串A和B转换为所有小写。对于字符串A中的每个字母,调用A和B上的函数 这是我目前掌握的代码: #include <iostream> #include <cstring> #include <cctype> using namespace std; char tolower(char c) { return c; } int countCh
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
char tolower(char c) {
return c;
}
int countChars(const char* string, char ch)
{
int count = 0;
int i;
int length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == ch)
{
count++;
}
}
return count;
}
int main() {
char stringA[50];
char stringB[50];
cout << "Please enter first string: ";
cin.getline(stringA, 50);
cout << "Please enter second string: ";
cin.getline(stringB, 50);
cout << "String 1: " << stringA << endl;
cout << "String 2: " << stringB << endl;
if (strcmp(stringA, stringB) == 0) {
cout << "These strings are anagrams" << endl;
}
else {
cout << "These strings are not anagrams" << endl;
}
}
如果你想用一种简单易读的方式来做,请考虑下面的代码:
#include <string>
#include <iostream>
#include <algorithm>
bool is_anagram(std::string s1, std::string s2)
{
std::sort(s1.begin(), s1.end());
std::sort(s2.begin(), s2.end());
return s1 == s2;
}
int main(void)
{
std::string s1, s2;
std::cout << "Input first string: ";
getline(std::cin, s1);
std::cout << "Input second string: ";
getline(std::cin, s2);
if (is_anagram(s1, s2))
std::cout << "Strings are anagrams." << std::endl;
else
std::cout << "Strings are NOT anagrams." << std::endl;
return 0;
}
当算法库为其使用的排序字符串相同时,函数is_anagram返回true
注意:字符串区分大小写。你好和你好是不同的
希望对你有帮助D只需对两个字符串进行排序,然后进行比较 索特斯特林加,斯特林加+斯特林斯廷加 sortstringB,stringB+strlenstringB 就在if语句之前 您正在检查这两个字符串是否完全相等。但字谜可以有不同的字母排列,但必须有相同的字母
虽然需要检查,但如果不考虑字符串本身是一个字谜,则可能需要检查这2个字符串是否相同。
PS-谷歌搜索会立即为您提供答案。最简单的方法是计算每个字母的数量。注意,你不需要数一数每一个字母,只要是字符串中的字母。要进行比较,请将结果存储在地图中
#include <iostream>
#include <string>
#include <map>
#include <array>
class AnagramCheck
{
// I am going to be lazy and use map to store the counts.
// But you can do this much smarter and get a better complexity
// by using an array. But that involves more checks
std::map<char, int> data;
public:
explicit AnagramCheck(std::string const& str)
{
for(auto x: str) {
++data[std::tolower(x)]; // If you want to be smart ignore space/punctuation
}
}
bool operator==(AnagramCheck const& rhs) const
{
return data == rhs.data;
}
};
int main()
{
std::string line1;
std::getline(std::cin, line1);
AnagramCheck line1Check(line1);
std::string line2;
std::getline(std::cin, line2);
AnagramCheck line2Check(line2);
if (line1Check == line2Check) {
std::cout << "Anagram\n";
}
}
countChars函数看起来不错,但您没有在代码中使用它。如果你从不使用它,写它有什么意义?显然,您要做的是使用该函数来帮助确定这两个字符串是否为字谜。您的说明甚至为您提供了一个大的线索,如何为字符串a中的每个字母执行此操作,调用a和B上的函数。欢迎使用堆栈溢出!请你的问题更具体一些。如前所述,您尚未说明问题所在或您遇到的问题。另请参见:,strcmp将比较是否相等,而不是字谜:aba!=baa但他们是一种幻觉。我认为要想解决这个问题,你需要解释一下你到底在做什么。为什么你不能按照问题告诉你的去做?你的问题是什么?1如何检查2个字符串是否为字谜?2如何计算字符串中字符的外观?3如何将字符串转换为小写?它们都是有趣的问题,但请选择.sort最多在.logn复杂性上。可以使用On进行字谜测试,而且简单的排序假设空格是重要的。在多字字谜中,您通常会忽略空格。@MartinYork:计数排序的复杂性:删除空格、更改大小写的预处理可以在On中完成。排序最多只能在On.logn复杂性中完成。可以使用On进行字谜测试,而且简单的排序假设空格是重要的。在多字字谜中,你通常忽略空格。大家都同意,但这个解决方案是在他已经存在的代码基础上增加两行代码。所以我觉得这样更好。但我会修改我的答案。