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

时间:2015-11-17 11:34:42

标签: c++ string loops

我对编程非常陌生,所以这是一个非常“混乱”/“脏”的代码。

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

e.g。

ASDFGHJKL和PFUYASD

我想输出字母匹配的位置:

“在Strand 2的第1和第6行找到匹配”

条件:

  1. 他们必须最多匹配三个并排字符。 (在示例中不考虑F的原因)
  2. 钢绞线1长于钢绞线2
  3. 所以我得到了这个代码,用于查找第二个字母的匹配。这很好用

    #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 << "string1\n";
        cin >> str1;
        cout << "string2\n";
        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;
    }
    

    但是我需要代码才能找到匹配,直到至少第3个字母,所以我想通过添加更多的嵌套循环它会起作用,但事实并非如此。这是代码不起作用:

    #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;
    }
    

1 个答案:

答案 0 :(得分:1)

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

开始查找子字符串
相关问题