programming-examples/c/String/C Program to Display Every Possible Combination of Two Words from the given 2 String without Displaying Repeated Combinations.c
2019-11-15 12:59:38 +01:00

96 lines
2.7 KiB
C

/*
* C Program to Display Every Possible Combination of Two Words
* from the given 2 String without Displaying Repeated Combinations
*/
#include <stdio.h>
#include <string.h>
void main()
{
char str1[50], str2[50], str3[100][100], str4[100][100];
char str5[200][200], temp[200], str[200][200];
int i, j = 0, k = 0, l = 0, m = 0, index = 0, n = 0;
printf("Enter first string
");
scanf("%[^
]s", str1);
printf("Enter second string
");
scanf(" %[^
]s", str2);
/* code to convert string in 2-D array */
for (i = 0; str1[i] != ''; i++)
{
if ((str1[i] = = ' ')
{
str3[j][k] = '';
j++;
k = 0;
}
else
{
str3[j][k] = str1[i];
k++;
}
str3[j][k] = '';
}
k = 0;
for (i = 0; str2[i] != ''; i++)
{
if ((str2[i] == ' ')
{
str4[l][k] = '';
l++;
k = 0;
}
else
{
str4[l][k] = str2[i];
k++;
}
str4[l][k] = '';
}
/* Code to make the first string words combination with second */
for (i = 0; i <= j; i++)
{
for (m = 0; m <= l; m++)
{
strcpy(temp, str3[i]);
strcat(temp, str4[m]);
strcpy(str5[index], temp);
index++;
}
}
/* Code to make the second string words combination with first */
for (i = 0; i <= l; i++)
{
for (m = 0; m <= j; m++)
{
strcpy(temp, str4[m]);
strcat(temp, str3[i]);
strcpy(str5[index], temp);
index++;
}
}
/* Code to remove the repetitions */
for (i = 0; i <= index; i++)
{
for (j = i + 1; j <= index; j++)
{
if ((strcmp(str5[i], str5[j]) == 0)
{
for (k = j; k <= index; k++)
{
strcpy(str5[k], str5[k + 1]);
}
index--;
}
}
}
for (i = 0; i <= index; i++)
{
printf("%s
", str5[i]);
}
}