-1

I am VERY new to programming so this is a very 'messy'/'dirty' code.

Situation is, if I got 2 strings

e.g.

ASDFGHJKL and PFUYASD

I would like to output their positions where letters match like this:

"Match found at 0 of Strand 1 and 6 of Strand 2"

Conditions:

  1. They must match upto three side by side characters. (the reason why the F isn't considered in the example)
  2. Strand 1 is longer than Strand 2

So I got this code that works for finding match up to second letter. This works fine

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

But I needed the code to find match until atleast 3rd letter so i thought just by adding more nested loop it will work but it doesn't. here's is the code that doesn't work:

#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;
}
Han Kenny
  • 5
  • 4
  • Loops implemented via `goto` statements is not what today's programmers want to see in a code. – LogicStuff Nov 17 '15 at 11:54
  • Your problem can be solved easily using `loop`. Your first task should be to learn more about loops and to read this: http://stackoverflow.com/questions/46586/goto-still-considered-harmful – Jérôme Nov 17 '15 at 11:54
  • Are you allowed to have repeats of a match? As in, __strand1__ = ASDASD and __strand2__ = FGHASD. Results 0 in s1 and 3 in s2 & 3 in s1 and 3 in s2? – tkellehe Nov 17 '15 at 11:57
  • @TMKelleher no, just one (first)instance – Han Kenny Nov 17 '15 at 12:02
  • @HanKenny, you might want to put that into the question because the current answer will do what I said. – tkellehe Nov 17 '15 at 12:06

1 Answers1

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 finds a substring starting from i

m7mdbadawy
  • 920
  • 2
  • 13
  • 17
  • Your answer fits the question, but the OP just said down in the comments to have no repeats in the search:) +1 – tkellehe Nov 17 '15 at 12:09
  • @m7mdbadwy where could i put the "match not found" if I cant find match between strings? – Han Kenny Nov 17 '15 at 12:10
  • @TMKelleher it doesnt actually matter because the input isnt just random characters, they will be two strings with very unique characters except they overlap at one point. can't explain properly. sry english is not my first language – Han Kenny Nov 17 '15 at 12:12
  • @Han Kenny so you want only to find a one match ? – m7mdbadawy Nov 17 '15 at 12:13
  • @m7mdbadawy . the inputs would be two strings with very unique characters except at one part where they got at least three identical characters so it will only find one match – Han Kenny Nov 17 '15 at 12:17
  • @Han Kenny I edited the code for no match and to stop when it finds a match – m7mdbadawy Nov 17 '15 at 12:23