summaryrefslogtreecommitdiff
path: root/combinations.c
diff options
context:
space:
mode:
authorjstmax! <maxwasmailed@proton.me>2025-11-02 19:19:52 +0100
committerjstmax! <maxwasmailed@proton.me>2025-11-02 19:19:52 +0100
commitd17a8b6aa9cb128c47d7f6c5414a786e336e430c (patch)
tree9546519d2e40f7c3d650e0ebe5850002f7284e7b /combinations.c
parentcd959eaa7f68fbce1c7a874c82ad8142aefb941a (diff)
tweaks + added custom coloumn size and usage screen
Diffstat (limited to 'combinations.c')
-rw-r--r--combinations.c46
1 files changed, 41 insertions, 5 deletions
diff --git a/combinations.c b/combinations.c
index 5237647..f115504 100644
--- a/combinations.c
+++ b/combinations.c
@@ -6,16 +6,32 @@
void swap(char *a, char *b);
void permute(char *s, int l, int r);
unsigned long long fact(int n);
+void usage();
const char red[] = "\033[31m";
const char white[] = "\033[37m";
const char norm[] = "\033[0m";
+int colsize = 3;
int main(int argc, char **argv) {
- if (argc != 2) {
+ if (argc < 2) {
printf("%scmb%s>%s at least give me a string :(\n", red, white, norm);
return 1;
}
+ if (argc == 3) {
+ if (strcmp(argv[2], "-h") == 0 || strcmp(argv[2], "--help") == 0) {
+ usage();
+ return 0;
+ } else {
+ if (atoi(argv[2]) > 9) {
+ colsize = 9;
+ } else if (atoi(argv[2]) < 1) {
+ colsize = 1;
+ } else {
+ colsize = atoi(argv[2]);
+ }
+ }
+ }
char warn = 'n';
char *str = strdup(argv[1]);
@@ -35,8 +51,9 @@ int main(int argc, char **argv) {
}
}
- printf("%scmb%s>%s ", red, white, norm); // without \n -> prints the given string, *wink* *wink* :)
+ printf("%scmb%s>%s %s\n", red, white, norm, str);
permute(str, 0, strlen(str) - 1);
+ printf("\n");
free(str);
return 0;
@@ -49,15 +66,18 @@ void swap(char *a, char *b) {
}
void permute(char *s, int l, int r) {
+ static unsigned long long count = 0;
if (l == r) {
- printf("%s\n", s);
+ printf("%s\t", s);
+ count++;
+ if (count % colsize == 0) printf("\n");
return;
}
for (int i = l; i <= r; i++) {
- swap(&s[l], &s[i]);
+ swap(&s[l], &s[i]);
permute(s, l + 1, r);
- swap(&s[l], &s[i]);
+ swap(&s[l], &s[i]);
}
}
@@ -65,3 +85,19 @@ unsigned long long fact(int n) {
return (n <= 1) ? 1 : n * fact(n - 1);
}
+void usage() {
+ // the most beautiful printf call you'll ever see <3
+ printf(
+ "%sC%so%sMB%sinations\n"
+ "A simple utility that prints all the combinations of a given string.\n\n"
+ "Syntax: %scmb%s string [%soptions%s]\n"
+ "%sOptions%s:\n"
+ "%s-h%s | %s--help%s : Prints this usage screen\n"
+ "%s1%s-%s9%s : Specify coloumn size\n"
+ , red, norm, red, norm
+ , red, norm, red, norm
+ , red, norm
+ , red, norm, red, norm
+ , red, norm, red, norm
+ );
+}