/* Wordshuffler
** written by Ralf Gruner, Großschönau, Germany
** ralf.gruner@t-online.de
** 27-aug-1999
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxchar 100

#define PROGRAM_NAME "wordshuffler"
#define VERSION_NUM "1.0"
#define VERSION_DAT "(27.8.99)"

char *help="Wordshuffler by Ralf Gruner.\nUsage: "PROGRAM_NAME" <word> [maximum consonants] [maximum vowels]";
const char *version="$VER: "PROGRAM_NAME" "VERSION_NUM" "VERSION_DAT;

int number,index,level,maxcons=2, maxvowels=2;
char array[maxchar], text[maxchar], character, mask[maxchar];
char *vowels="AEIOUaeiouÄÖÜäöüy";

void permute(void)
{
	int index, conscount=0, vowelcount=0, isvowel, i, j;

	level++;
	for(index=0; index<number; index++)
	{
		if(mask[index])
		{
			text[level]=array[index];
			mask[index]=0;
			permute();
			mask[index]=1;
			if(level==number-1)
			{
				for(i=0; i<number; i++)
				{
					if(conscount>maxcons)
						break;
					conscount++;

					for (j=0; vowels[j]!='\0'; j++)
					{
						if(text[i]==vowels[j])
							conscount=0;
					}
				}

				for(i=0; i<number; i++)
				{
					if(vowelcount>maxvowels)
						break;

					isvowel=0;
					for (j=0; vowels[j]!='\0'; j++)
					{
						if(text[i]==vowels[j])
							isvowel=1;
					}

					if(isvowel)
						vowelcount++;
					else
						vowelcount=0;
				}

				if(conscount<=maxcons && vowelcount<=maxvowels)
				{
					for(i=0; i<number; putchar(text[i]), i++);
					putchar('\n');
				}
			}
		}
	}
	level--;
}

main(int argc, char *argv[])
{
	int test=1, i;

	if(argc>1)
	{
		if(argc>2)
		{
			maxcons=atoi(argv[2]);
		}
		if(argc>3)
		{
			maxvowels=atoi(argv[3]);
		}
		level=-1;
		number=strlen(argv[1]);
		if(number>=maxchar) exit(10);
		strcpy(array, argv[1]);
	
		while(test)
		{	/* sort */
			test=0;
			for(i=0; i<number-1; i++)
			{
				if(array[i]>array[i+1])
				{
					character=array[i];
					array[i]=array[i+1];
					array[i+1]=character;
					test=1;
				}
			}
		}
	
		for (index=0; index<number; index++)
			mask[index]=1;
		permute();
	}
	else
		puts(help);
}

