Okay, I'm so close to getting this done but I've seem to hit a snag.
This program takes two strings and compares them and see what substrings both of them contain. I think it's better if I show it..
for example:
Enter Two strings.
zan
can
z is index 0, a is index 1 and n is index 2
Code:
INDEX LENGTH MSS
0 0
1 2 an
2 1 a
Onto the problem with mine, right now it will only output when the substrings don't match. I'm hoping someone can point out to me a stupid error I made, I would appreciate it. Also I have traced the program and it works so that's why I'm here. Anyway here's my code:
Code:
/*
1. Maximum Shared Substring
Description: A program that finds the MSS for two strings that are at most 1000 characters long.
File: main.cxx
Programmer: ****
E-mail: ***
Compiler: g++
Last modified: 4/3/05
*/
using namespace std;
#include <iostream>
#include <string>
#include <iomanip>
void MMS(string strOne, string strTwo, int i);
void outputINDEX(int i);
void outputLENGTH(int subStringLength);
void outputMSS(string subString);
int main()
{
string strOne, // Used to input first string
strTwo; // Used to input second string
cout << "Enter two strings." << endl << endl;
cin >> strOne >> strTwo;
cout << "INDEX" << setw(9) << "LENGTH" << setw(6) << "MSS" << endl << endl;
return 0;
}
void MMS(string strOne, string strTwo, int i)
{
char strOneLett; // Used to store the letter for testing
int strTwoAns, // Used to store if a letter from string one is in string two
subStringLength = 0, // Used to store length of the subString
strOneLength = strOne.length() - 1, //Used to store length of stringOne - 1
x = 0, // Used as loop variable for the second while loop
i = 0; // Used as loop variable for the first while loop
string subString = "";
while(i <= strOneLength)
{
strOneLett = strOne.at(i);
strTwoAns = strTwo.find(strOneLett);
// When the letter is not found in string 2 it will output a blank row
if(strTwoAns == -1)
{
outputINDEX(i);
outputLENGTH("0");
cout << endl;
}
// When the letter is found in string 2 it will see if it's only a shared charaacter or a substring
if(strTwoAns != -1)
{
subString = "";
while(x <= strOneLength)
{
subString = subString + strOne.at(x) ;
strTwoAns = strTwo.find(subString);
if(strTwoAns != -1 && x != strOneLength)
{
}
if(strTwoAns == -1)
{
subString = subString - strOne.at(x);
subStringLength = subString.length();
outputINDEX(x);
outputLENGTH(subStringLength);
outputMSS(subString);
}
if(strTwoAns != -1 && x == strOneLength)
{
subStringLength = subString.length();
outputINDEX(i);
outputLENGTH(subStringLength);
outputMSS(subString);
}
x++;
}
}
i++;
}
}
// Output Functions
void outputINDEX(int i)
{
cout << setw(5) << setiosflags(ios::right) << i;
}
void outputLENGTH(int subStringLength)
{
cout << setw(9) << setiosflags(ios::right) << subStringLength;
}
void outputMSS(string subString)
{
int width = subString.length() + 3;
cout << setw(width) << setiosflags(ios::left) << subString << endl;
}