Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编写一个名为find_mismatch的函数,该函数接受两个字符串作为输入参数_C++ - Fatal编程技术网

C++ 编写一个名为find_mismatch的函数,该函数接受两个字符串作为输入参数

C++ 编写一个名为find_mismatch的函数,该函数接受两个字符串作为输入参数,c++,C++,我试图写这个函数,但我被卡住了。只是想在春假的时候做些练习。如果有人有一个全新的方法,我是开放的。我知道这个代码是一个令人悲伤的景象,但我不能把我的头围绕它。提前谢谢 如果两个字符串完全匹配,则为0 1如果两个字符串长度相同且仅在一个字符中不匹配 2如果两个字符串的长度不相同或两个或多个字符不匹配 代码: void findMismatch(常量字符串s1、常量字符串s2) {int计数; 如果(s1.length()!=s2.length()) { cout这是我提出的一个解决方案 #in

我试图写这个函数,但我被卡住了。只是想在春假的时候做些练习。如果有人有一个全新的方法,我是开放的。我知道这个代码是一个令人悲伤的景象,但我不能把我的头围绕它。提前谢谢

  • 如果两个字符串完全匹配,则为0
  • 1如果两个字符串长度相同且仅在一个字符中不匹配
  • 2如果两个字符串的长度不相同或两个或多个字符不匹配
代码:

void findMismatch(常量字符串s1、常量字符串s2)
{int计数;
如果(s1.length()!=s2.length())
{

cout这是我提出的一个解决方案

#include <iostream>
#include <string>

using namespace std;
void findMismatch(string s1, string s2)
{   

    if(s1.length() != s2.length())
    {
        cout<<"2"<<endl;
    }
    else if(s1 == s2) //exact match
    {
        cout<<"0"<<endl;
        return;
    }
    else
    {

        int mismatchCount = 0;
        int i = 0;
        while(i < s1.length() && mismatchCount<=1)
        {
            if(s1.at(i) != s2.at(i) && ++mismatchCount == 2)
            {
                cout<<"2"<<endl;
                return;
            }
            i++;
        }
        cout<<"1"<<endl;
    }


}
int main()
{
  findMismatch("Achr","Acha"); //one mismatch
  findMismatch("Achrfs","Achaee"); //two or more mismatch
  findMismatch("Ach","Ach"); //exact match
  findMismatch("Achrfdfd","Acha"); //different lengths
}

#包括
#包括
使用名称空间std;
void findMismatch(字符串s1、字符串s2)
{   
如果(s1.length()!=s2.length())
{

究竟是什么?应该被编辑吗?很抱歉刚刚被编辑。你被要求写一个名为“FuntMySuffMatt”的函数,你把它命名为“FixMeunMatt”?我想你需要写一个循环来计算有多少个字符匹配,有多少个不匹配(并且考虑正确地缩进代码-更容易让别人阅读)。
#include <iostream>
#include <string>

using namespace std;
void findMismatch(string s1, string s2)
{   

    if(s1.length() != s2.length())
    {
        cout<<"2"<<endl;
    }
    else if(s1 == s2) //exact match
    {
        cout<<"0"<<endl;
        return;
    }
    else
    {

        int mismatchCount = 0;
        int i = 0;
        while(i < s1.length() && mismatchCount<=1)
        {
            if(s1.at(i) != s2.at(i) && ++mismatchCount == 2)
            {
                cout<<"2"<<endl;
                return;
            }
            i++;
        }
        cout<<"1"<<endl;
    }


}
int main()
{
  findMismatch("Achr","Acha"); //one mismatch
  findMismatch("Achrfs","Achaee"); //two or more mismatch
  findMismatch("Ach","Ach"); //exact match
  findMismatch("Achrfdfd","Acha"); //different lengths
}