Skip to content

Instantly share code, notes, and snippets.

@thomashw
Created February 19, 2012 07:52
Show Gist options
  • Save thomashw/1862514 to your computer and use it in GitHub Desktop.
Save thomashw/1862514 to your computer and use it in GitHub Desktop.
The program will generate all permutations of the input string.
// Created by: Thomas Hewton-Waters
// Date: February 18, 2012
/* The program will generate all permutations of the input string. */
#include <stdio.h>
#include <string.h>
void merge( char end, char str[], char perms[][100] );
void permutate( char str[], int index, int *num_perms );
int main()
{
char str[105];
int x, *num_perms;
num_perms = &x;
*num_perms = 0;
printf( "Please enter letters to be permutated: " );
scanf( "%100s", str );
printf( "Permutating %s", str );
permutate( str, 0, num_perms );
printf( "Total number of permutations: %d\n", *num_perms );
}
void permutate( char str[], int index, int *num_perms )
{
int i = 0;
char temp;
if( index == strlen(str) - 1 ) {
(*num_perms)++;
printf( "%s\n", str);
return;
}
for( i = index; i < strlen(str); i++ ) {
temp = str[i];
str[i] = str[index];
str[index] = temp;
permutate( str, index + 1, num_perms );
temp = str[i];
str[i] = str[index];
str[index] = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment