hi
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
#include <string.h>
using namespace std;
#define SIZE 100
void reset(char temp[]) {
for (int i=0;i <= strlen(temp);i++)
temp[i] = 0;
}
void lcs(char str1[], char str2[], char temp[], int x, int y, int i)
{
if (str1[x]==0)return;
else
{
if (str1[x]==str2[y]){
temp[i]=str1[x];if ((str1[x+1]!=0) && (str2[y+1]!=0))return lcs (str1, str2, temp,x+1 ,y+1 , i+1);}
elseif (str2[y] == 0)return lcs (str1, str2, temp,x+1 ,strlen(temp), i+1);if (str2[y+1] != 0)return lcs (str1, str2, temp, x, y + 1, i);
if (str1[x] == 0)return lcs (str1, str2, temp, strlen(temp), y, i);
}
}
void main()
{
char str1[SIZE]={0},str2[SIZE]={0},temp[SIZE]={0}, big[SIZE] = {0};cout<<"Please enter the first string:";
cin>>str1;
cout<<"Please enter the second string:";
cin>>str2;
lcs(str1, str2, temp, 0, 0, 0);
for(int i=0;i<SIZE;i++)
big[i]=temp[i];
reset(temp);
lcs(str2, str1, temp, 0, 0, 0);
cout<<"The longest common string is:";
if ((strlen(big)) > (strlen(temp)))
cout<<big<<endl;
else
cout<<temp;
}
Above code returns
a
, but when the inputs are:
Hgaksc
Agsmcdf
it should return
gsc
.
Please help me.
I can't find it's problem
Thanks