Write a program that reads n strings (with blank spaces).
For each read string, check if it is a permutation of some other read string.
If it is, print the found string and all other found strings that are permutations of the found string.
Uppercase and lowercase letter are not the same.
Example input: n=3
1. string: programming is fun
2. string: mmni sfu ragrop ngi
3. string: Programming Is Fun
Output:
programming is fun
mmni sfu ragrop ngi
I defined the function for checking if two strings are permutations as following:
#include <stdio.h>
#include <stdlib.h>
#define MAX_PERMUTATIONS 5
#define MAX_LINE_SIZE 128
void read(char ***arr,int *n);
int arePermutation(char *str1,char *str2);
void read(char ***arr,int *n)
{
do
{
printf("n=");
scanf("%d",n);
}
while(*n < 1);
arr=(char***)malloc(MAX_PERMUTATIONS * sizeof(char**));
int i,j;
for(i=0; i<MAX_PERMUTATIONS; i++)
{
arr[i]=(char**)malloc(*n * sizeof(char*));
for(j=0; j<*n; j++)
arr[i][j]=(char*)malloc(MAX_LINE_SIZE * sizeof(char));
}
for(i=0; i<MAX_PERMUTATIONS; i++)
{
for(j=0; j<*n; j++)
{
fflush(stdin);
gets(arr[i][j]);
}
}
}
int arePermutations(char *str1,char *str2)
{
int count1[MAX_LINE_SIZE]={0},count2[MAX_LINE_SIZE]={0};
int i;
for(i=0; str1[i] && str2[i]; i++)
{
count1[str1[i]]++;
count2[str2[i]]++;
}
if(str1[i] || str2[i])
return 0;
for(i=0; i<MAX_LINE_SIZE; i++)
{
if(count1[i] != count2[i])
return 0;
}
return 1;
}
How to check for n permutations and print them?