C Program to Print All Possible Combination of Two Words from the String without any Repetition

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.

  1. /*
  2.  * C Program to Display Every Possible Combination of Two Words 
  3.  * from the given 2 String without Displaying Repeated Combinations
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     char str1[50],  str2[50], str3[100][100], str4[100][100];
  11.     char str5[200][200], temp[200], str[200][200];
  12.     int i, j = 0, k = 0, l = 0, m = 0, index = 0, n = 0;
  13.     printf("Enter first string\n");
  14.     scanf("%[^\n]s", str1);
  15.     printf("Enter second string\n");
  16.     scanf(" %[^\n]s", str2);    
  17.  
  18. /* code to convert string in 2-D array */
  19.     for (i = 0;str1[i] != '\0';i++)
  20.     { 
  21.         if ((str1[i] =  = ' ')
  22.         {
  23.             str3[j][k] = '\0';
  24.             j++;
  25.             k = 0;
  26.         }
  27.         else
  28.         {
  29.             str3[j][k] = str1[i];
  30.             k++;
  31.         }
  32.         str3[j][k] = '\0';    
  33.     }
  34.     k = 0;
  35.  
  36.     for (i = 0;str2[i] != '\0';i++)
  37.     {
  38.         if ((str2[i] == ' ')
  39.         {
  40.             str4[l][k] = '\0';
  41.             l++;
  42.             k = 0;
  43.         }
  44.         else
  45.         {
  46.             str4[l][k] = str2[i];
  47.             k++;
  48.         }
  49.         str4[l][k] = '\0';
  50.     }
  51. /* Code to make the first string words combination with second */
  52.     for (i = 0;i <= j;i++)
  53.     {
  54.         for (m = 0;m <= l;m++)
  55.         {
  56.             strcpy(temp, str3[i]);
  57.             strcat(temp, str4[m]);
  58.             strcpy(str5[index], temp);
  59.             index++;
  60.         }
  61.     }    
  62.  
  63. /* Code to make the second string words combination with first */
  64.     for (i = 0;i <= l;i++)
  65.     {
  66.         for (m = 0;m <= j;m++)
  67.         {
  68.             strcpy(temp, str4[m]);
  69.             strcat(temp, str3[i]);
  70.             strcpy(str5[index], temp);
  71.             index++;
  72.         }
  73.     }
  74.  
  75. /* Code to remove the repetitions */
  76.     for (i = 0;i <= index;i++)
  77.     {
  78.         for (j = i + 1;j <= index;j++)
  79.         {
  80.             if ((strcmp(str5[i], str5[j]) == 0)
  81.             {
  82.                 for (k = j;k <= index;k++)
  83.                 {
  84.                     strcpy(str5[k], str5[k + 1]);
  85.                 }
  86.                 index--;
  87.             }
  88.         }
  89.     }
  90.     for (i = 0;i <= index;i++)
  91.     {
  92.         printf("%s\n", str5[i]);
  93.     }
  94. }

$ 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.

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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.