Programming / Computing

String Compression

 This projects takes two strings as input and returns the most shortest string by joining both the input strings ...............................................................................................................................................................................

 

 

 

 

#include<iostream.h>

#include<conio.h>

#include<string.h>

 

int main()

{

char a[50],b[50],c[100] = "",at[50],bt[50];

int l1,l2,max1=0,max2=0,i,j;

cout<<"enter the first word";

cin>>a;

cout<<"enter the second word";

cin>>b;

 

l1 = strlen(a);

l2 = strlen(b);

 

 

if(strstr(a,b))

strcpy(c,a);

else if(strstr(b,a))

strcpy(c,b);

else if((a[l1] == b[0]) || (a[0] == b[l2]))

{

if(a[l1] == b[0])

{

for(i=l1,j=0;i>=0;i--,j++)

{

if(a[i] != b[j])

break;

else

max1++;

}

}

 

if(a[0] == b[l2])

{

for(i=l2,j=0;i>=0;i--,j++)

{

if(b[i] != a[j])

break;

else

max2++;

}

}

 

if(max1>=max2)

{

int t = l2-max1;

for(i=0;i<=t;i++)

{

bt[i] = b[i + max1];

}

strcpy(c,a);

strcat(c,bt);

}

else

{

for(i=0;i<=(l1-max2);i++)

{

at[i] = a[i+max2];

}

strcpy(c,b);

strcat(c,at);

}

}

else

{

strcpy(c,a);

strcat(c,b);

}

 

cout<<"the output string is"<<c;

 

return 0;

}

© 2010 All rights reserved.

Make a free websiteWebnode