Display
Every Possible Combination of Two Words from the given 2 String without
Displaying Repeated Combinations
#include
#include
#include
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;
clrscr();
printf("Enter first string\n");
scanf("%[^\n]s", str1);
printf("Enter second string\n");
scanf(" %[^\n]s", str2);
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';
}
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++;
}
}
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++;
}
}
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]);
}
getch();
}
Output
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
Accept 2 String & check whether all Characters in first String
is Present in second String & Print
#include
#include
#include
#include
#include
#define char_size 26
void alphacheck(char *, int []);
void create(char *, int[]);
int main()
{
char str1[50], str2[50];
int a1[Char_size] = {0}, a2[Char_size] = {0}, i;
char str1_alpha[Char_size], str2_alpha[Char_size];
clrscr();
printf("Enter string1: ");
scanf("%s", str1);
printf("Enter string2: ");
scanf("%s", str2);
alphacheck(str1, a1);
alphacheck(str2, a2);
create(str1_alpha, a1);
create(str2_alpha, a2);
if (strcmp(str1_alpha, str2_alpha) == 0)
{
printf("All characters match in %s and %s.\n", str1, str2);
printf("The characters that match are: ");
for (i = 0; i < strlen(str1_alpha); i++)
{
printf("%c, ", str1_alpha[i]);
}
printf("\n");
}
else
{
printf("All characters do not match in %s and %s.\n", str1, str2);
}
return 0;
}
void alphacheck(char *str, int a[])
{
int i, index;
for (i = 0; i < strlen(str); i++)
{
str[i] = tolower(str[i]);
index = str[i] - 'a';
if (!a[index])
{
a[index] = 1;
}
}
}
void create(char *str, int a[])
{
int i, j = 0;
for (i = 0; i < Char_size; i++)
{
if (a[i])
{
str[j++] = i + 'a';
}
}
str[j] = '\0';
}
Output
Enter string1: aspired
Enter string2: despair
All characters match in aspired and despair.
The characters that match are: a, d, e, i, p, r, s,
No comments:
Post a Comment