我想知道如何创建一个可以接受输入的功能,例如" Computre"或任何可能性......" Cmoputer," " romputeC"而不是"计算机"。
//This function would be something like this:
void ignore_switched_letters(char word[]);
int main(){//...}
void ignore_switched_letters(char word[]){
//... code implimentation...
char computer[9] = "Computer";
int length = strlen(computer);
for (int i = 0; i < length; ++i){
if (word[i] == 'C'){ ....
}
}
}
答案 0 :(得分:2)
我建议你将这个单词放在spyOn($filter,'BaseUrl').andReturn('http://localhost');
expect(scope.getLink()).toEqual('http://localhost/test.html');
中以小写字母表示。然后你可以使用standard library algorithms的强大功能,包括std::mismatch
,它返回两个范围不同的第一个位置。您可以检查是否存在两个彼此相反的不匹配。将它编写为比较两个单词的函数可能更灵活:
std::string
答案 1 :(得分:0)
这或多或少是(1)比较astr
和bstr
,直到找到差异为止。 (2)记住差异(3)继续比较,直到找到第二个差异(4)检查第二个差异是否与第一个差异的倒数相匹配(5)继续比较。
int main(int argc,char ** argv) {
std::string astr="Computer";
if(argc==2) {
std::string bstr(argv[1]);
// Not the same length == no permutation
if(astr.size()!=bstr.size()) {
std::cerr << "nope" << std::endl;
return 1;
}
int i;
for(i=0;i<astr.size();i++) {
if(astr[i]!=bstr[i]) {
break;
}
}
// We iterated thru == strings are equal
if(i==astr.size()) {
std::cerr << "yes" << std::endl;
return 0;
}
// We have found the one different char
// continue iteration and search for the
// second difference
int j;
for(j=i+1;j<astr.size();j++) {
// another difference
if(astr[j] != bstr[j]) {
// it is the one from astr and vice versa
if(astr[i] == bstr[j] && astr[j] == bstr[i]) {
break;
}
// No it isn't
std::cerr << "*nope" << std::endl;
return 1;
}
}
// Didn't find the second difference
if(j==astr.size()) {
std::cerr << "nope" << std::endl;
return 1;
}
j++;
// All others have to be identical
for(;j<astr.size();j++) {
if(astr[j] != bstr[j]) {
std::cerr << "nope" << std::endl;
return 1;
}
}
std::cerr << "yes" << std::endl;
return 0;
}
}
答案 2 :(得分:0)
所有这些都非常复杂。你应该坚持手头的实用程序。我强烈建议切换到std::string
界面并使用标准库算法。
以下是我如何解决您的问题:
void ignore_switched_letters(const std::string &word){
static std::string computer = "computer";
if(!std::is_permutation(std::begin(computer), std::end(computer), std::begin(word)))
throw std::runtime_error("string is not a permutation of 'computer'");
// do whatever you like with 'word'
}
当然,你不必抛出异常;但是,如果表现不是一个问题,我喜欢。
更通用的解决方案可能更好:
template<typename Functor>
void ignore_switched_letters(const std::string &check, const std::string &word, Functor &&f){
if(!std::is_permutation(std::begin(check), std::end(check), std::begin(word)))
throw std::runtime_error(word + " is not a permutation of " check);
f();
}
然后您可以检查多个案例,如下所示:
int main(){
std::string computer = "computer";
std::string chair = "chair";
std::string microwave = "microwave";
try{
ignore_switched_letters(computer, "puterc", [](){
std::cout << "is a computer\n";
});
// throws 'std::runtime_error'
ignore_switched_letters(chair, "hair", [](){
std::cout << "is a chair\n";
});
ignore_switched_letters(microwave, "wavemicro", [](){
std::cout << "is a microwave\n";
});
}
catch(const std::exception &e){
std::cerr << e.what() << '\n';
exit(EXIT_FAILURE);
}
}
这是一个不错的小便利。
我跳了枪,错过了你只想要 2个字符的排列,但是我会留下我的答案。
答案 3 :(得分:0)
标准库有很多有用的算法:
bool compareOnePermutation(std::string s1, std::string s2)
{
if (!std::is_permutation(s1.begin(), s1.end(), s2.begin(), s2.end()))
{
return false; // strings contain different letters
}
// Find the differences
std::transform(s1.begin(),s1.end(),s2.begin(), s1.begin(), std::bit_xor<char>());
int equal = std:count(s1.begin(),s1.end(), char(0));
if (equal==s1.size()) return true; // s1==s2
if (equal==s1.size()-2) return true; // Permutation with 1 swap.
return false;
}
使用的技巧是char1 ^ char2 == 0当且仅当char1 == char2时。因此,在变换之后,我可以快速计算零。当然,自己编写代码会更有效率,因为你只需要计算差异,并在达到3时拯救:
// Find the differences
auto iter2 = s2.begin();
size_t diff = 0;
for (char c:s1) {
if (c != *iter2++) ++diff;
if (diff==3) return false;
}
return diff !=1;