Open orionrush opened 6 years ago
// Ref https://goo.gl/gvzist
The following recursion fails though it compiles and runs in other environments.
#include <ctype.h> #include <string.h> #include <stdio.h> #include <unistd.h> // Ref: //http://qr.ae/TUpcap int alpha_length=52; char alpha[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; static int count = -1; void permutations(char arr[], int arr_length, char prefix[], int places) { int i; int j; int plength=strlen(prefix); char newprefix[plength + 2]; // could be 1, being conservitive; // dont run the whole algorithm just enough to visualise recursion if(count > 0) { return; } if(places==0) { printf("%d %s\n", ++count, prefix); return; } for( i = 0; i < arr_length; i++) { //Concatenation of currentPrefix + arr[i] = newPrefix for(j = 0; j < plength; j++) { newprefix[j] = prefix[j]; } newprefix[plength] = arr[i]; newprefix[plength + 1] = '\0'; // Recursion permutations(arr, arr_length, newprefix, places - 1); } } int main() { permutations(alpha, alpha_length, "", 2); return 0; }
// Ref https://goo.gl/gvzist
The following recursion fails though it compiles and runs in other environments.