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:
- They must match upto three side by side characters. (the reason why the F isn't considered in the example)
- 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;
}