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?

Recommended Answers

All 4 Replies

When people start using 3 pointer redirections in a function (as you do with your read() function) my eyes roll back in my head and I tell the developer to KISS my ass! That's KISS in Keep It Simple, Stupid!

I see that DaniWeb's sanitization software has turned my reference to a donkey into a ### string... :-)

Until you do simplify it, I am not looking any further to help you, and if anyone else does, they deserve all the pain they will definitely suffer!

Here is the previous code with "simplified" allocation:

    #include <stdio.h>
    #include <stdlib.h>
    #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(n * sizeof(char*));
       int i;
       for(i=0; i<n; i++)
           arr[i]=(char *)malloc(MAX_LINE_SIZE * sizeof(char));
       for(i=0; i<n; i++)
       {
           fflush(stdin);
           printf("%d. line: ",i+1);
           gets(arr[i]);
       }
    }
    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;
    }

I would use a Cvec of dynamic C strings to hold all the strings read in from file.

See:

http://developers-heaven.net/forum/index.php/topic,2580.0.html

Then traversing the Cvec of strings
to see if any other string in the Cvec is a permutation of the string at this index ...

use a compare function that makes a sorted copy of each string to be compared ... then if sorted copies match ... you have a permutation.

OR ...

you may want to firstly make a copy of the Cvec of strings ...
then sort each string in that copy ...
then use the relevant index to compare strings while looking for permutations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.