From 22530a31b15544f36d84d6930fd96410c272f391 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 14 May 2026 23:05:49 +0200 Subject: [PATCH] Add rev.c for reversing strings and file lines Implement a command-line utility to reverse strings or lines from a file. --- src/userland/cli/rev.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/userland/cli/rev.c diff --git a/src/userland/cli/rev.c b/src/userland/cli/rev.c new file mode 100644 index 0000000..4d1262f --- /dev/null +++ b/src/userland/cli/rev.c @@ -0,0 +1,43 @@ +// Copyright (c) 2026 janevers (https://github.com/zeyadhost) +// This software is released under the GNU General Public License v3.0. See LICENSE file for details. +// This header needs to maintain in any file it is present in, as per the GPL license terms. + +#include +#include +#include +#include + +// reverse function +void reverse(char *str) { + int len = strlen(str); + for (int i = 0; i < len / 2; i++) { + char tmp = str[i]; + str[i] = str[len - 1 - i]; + str[len - 1 - i] = tmp; + } +} + +int main(int argc, char *argv[]) { + // check if there are too much flags + if (argc != 2) { + printf("Wrong input, use --help for more info\n"); + return 1; + } + + FILE *fp = fopen(argv[1], "r"); + // check if the argument is a file or not by opening it + if (fp == NULL) { + reverse(argv[1]); + printf("%s\n", argv[1]); + return 0; + } + // reverse if flag is a file + char line[1024]; + while (fgets(line, sizeof(line), fp)) { + line[strcspn(line, "\n")] = '\0'; + reverse(line); + printf("%s\n", line); + } + fclose(fp); + return 0; +}