This C Program Displays Every Possible Combination of Two Words from the given 2 String without Displaying Repeated Combinations.
Here is source code of the C Program to Display Every Possible Combination of Two Words from the given 2 String without Displaying Repeated Combinations. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* 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\n");
scanf("%[^\n]s", str1);
printf("Enter second string\n");
scanf(" %[^\n]s", str2);
/* code to convert string in 2-D array */
for (i = 0;str1[i] != '\0';i++)
{
if ((str1[i] = = ' ')
{
str3[j][k] = '\0';
j++;
k = 0;
}
else
{
str3[j][k] = str1[i];
k++;
}
str3[j][k] = '\0';
}
k = 0;
for (i = 0;str2[i] != '\0';i++)
{
if ((str2[i] == ' ')
{
str4[l][k] = '\0';
l++;
k = 0;
}
else
{
str4[l][k] = str2[i];
k++;
}
str4[l][k] = '\0';
}
/* 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\n", str5[i]);
}
}
$ cc program27.c $ a.out Enter first string welcome to sanfoundry's class Enter second string welcome to c programming class welcomewelcome welcometo welcomec welcomeprogramming welcomeclass towelcome toto toc toprogramming toclass sanfoundry'swelcome sanfoundry'sto sanfoundry'sc sanfoundry'sprogramming sanfoundry'sclass classwelcome classto classc classprogramming classclass cwelcome programmingwelcome cto programmingto welcomesanfoundry's tosanfoundry's csanfoundry's programmingsanfoundry's cclass programmingclass
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Buy C Books
- Apply for C Internship
- Practice BCA MCQs