Permutation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function Prototypes
void printWrapper(char str[]);
void printAll(char str[], char perm[], int used[], int k, int n);
// Calls recursive permutation function
void printWrapper(char str[]) {
int len = strlen(str);
char perm[len + 1];
perm[len] = '\0';
int used[len];
for (int i = 0; i < len; i++) {
used[i] = 0;
}
printAll(str, perm, used, 0, len);
}
// Recursively creates/ prints all permutations of char[] str
void printAll(char str[], char perm[], int used[], int k, int n) {
if (k == n) { // Base case - Stops when k reaches length of str
printf("%s\n", perm);
return;
}
for (int i = 0; i < n; i++) {
if (!used[i]) {
used[i] = 1;
perm[k] = str[i];
printAll(str, perm, used, k + 1, n);
used[i] = 0;
}
}
return;
}
int main(void) {
char str[] = "CAT";
printWrapper(str);
return 0;
}
Academic Notice: The code shown here is provided solely as a learning reference. Copying and pasting is intentionally disabled to encourage independent practice. Students should implement solutions on their own to demonstrate understanding. This material is not intended for direct submission in assignments.
Additionally: This code was written by a former CS1 student and may not reflect your professor’s intended solution or instructional approach. For coursework, students are expected to follow examples, conventions, and requirements presented in class and in professor-assigned materials.