查找字符串匹配并输出位置

Finding String Match and output the position

本文关键字:输出 位置 串匹配 字符 字符串 查找      更新时间:2023-10-16

我对编程非常陌生,所以这是一个非常"混乱"/"无聊"的代码。

情况是,如果我有两个字符串

例如

ASDFHJKL和PFUYASD

我想输出他们的位置,字母匹配如下:

"在1号股的0和2号股的6处发现匹配"

条件:

  1. 它们必须最多匹配三个并排的字符。(示例中未考虑F的原因)
  2. 股1比股2长

所以我得到了这个代码,它可以查找第二个字母的匹配项。这很好

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
    int x = 0;
    int y = 0;
    int str1match;
    int str2match;
    string str1;
    string str2;
    cout << "string1n";
    cin >> str1;
    cout << "string2n";
    cin >> str2;
    int length = str1.length();
startagain:
    int pos = str2.find(str1[x]);
    if ((pos >= 0) && (x<length))
    {
        x = x + 1;
        pos = pos + 1;
        if (str1[x] == str2[pos])
        {
            x = x + 1;
            pos = pos + 1;
            if (str1[x] == str2[pos])
            {
                str1match = x - 2;
                str2match = pos - 2;
                cout << "Match at " << str1match << " of Strand 1 and at " << str2match << " of Strand 2";
            }
            else
            {
                x = x + 1;
                goto startagain;
            }
        }
        else
        {
            x = x + 1;
            goto startagain;
        }
    }
    else if ((pos == -1) && (x<length))
    {
        x = x + 1;
        goto startagain;
    }
    else
    {
        cout << "Match not found";
    }
    _getch();
    return 0;
}

但我需要代码来找到匹配,直到至少第三个字母,所以我认为只要添加更多的嵌套循环就可以了,但事实并非如此。这是不起作用的代码:

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
    int x = 0;
    int str1match, str2match;
    string strand1, strand2;
    cout << "Enter Strand 1:n";
    cin >> strand1;
    cout << "Enter Strand 2:n";
    cin >> strand2;
    int length = strand1.length();
startagain:
    int pos = strand2.find(strand1[x]);
    if ((pos >= 0) && (x < length))
    {
        x = x + 1;
        pos = pos + 1;
        if (strand1[x] == strand2[pos])
        {
            x = x + 1;
            pos = pos + 1;
            if (strand1[x] == strand2[pos])
            {
                x = x + 1;
                pos = pos + 1;
                if (strand1[x] == strand2[pos])
                {
                    x = x + 1;
                    pos = pos + 1;
                    if (strand1[x] == strand2[pos])
                    {
                        str1match = x - 3;
                        str2match = pos - 3;
                        cout << "Match at " << str1match << "of Strand 1 and at " << str2match << "of Strand 2";
                    }
                    else
                    {
                        x = x + 1;
                        goto startagain;
                    }
                }
                else
                {
                    x = x + 1;
                    goto startagain;
                }
            }
            else
            {
                x = x + 1;
                goto startagain;
            }
        }
        else
        {
            x = x + 1;
            goto startagain;
        }
    }
    else if ((pos == -1) && (x < length))
    {
        x = x + 1;
        goto startagain;
    }
    else
    {
        cout << "Match not found";
    }
    _getch();
    return 0;
}
bool found = false;
for(int i=0;i<strand1.size()-2;i++){
    int pos = strand2.find(strand1.substr(i,3));
    if(pos != string::npos){
        found = true;
        cout << "match at " << i << "in 1 with " << pos << " in 2" << 'n';
        break;
    }
}
if (!found) cout << "No match";

string.substr查找从i 开始的子字符串