diff options
| author | Abigail <abigail@abigail.be> | 2021-01-26 12:22:55 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-26 12:22:55 +0100 |
| commit | e1e2237723cd0e85c69c1c53644ebe8c9614dc8a (patch) | |
| tree | d9b6b1aeb069c0397a26df46c438e28c590bdc91 | |
| parent | 31f71b49569afa8b719e15047bb0d2392bbb7993 (diff) | |
| download | perlweeklychallenge-club-e1e2237723cd0e85c69c1c53644ebe8c9614dc8a.tar.gz perlweeklychallenge-club-e1e2237723cd0e85c69c1c53644ebe8c9614dc8a.tar.bz2 perlweeklychallenge-club-e1e2237723cd0e85c69c1c53644ebe8c9614dc8a.zip | |
C solution for week 97, part 2
| -rw-r--r-- | challenge-097/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-097/abigail/c/ch-1.c | 50 |
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index 6d03a861aa..50f59dc1e2 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -26,6 +26,7 @@ to indicate the left shift. ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) diff --git a/challenge-097/abigail/c/ch-1.c b/challenge-097/abigail/c/ch-1.c new file mode 100644 index 0000000000..8219361e36 --- /dev/null +++ b/challenge-097/abigail/c/ch-1.c @@ -0,0 +1,50 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <unistd.h> +# include <ctype.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o -s SHIFT < input-file + */ + +int main (int argc, char ** argv) { + char * line = NULL; + size_t len = 0; + size_t strlen; + int ch; + int shift = -1; + int NR_OF_LETTERS = 26; + + while ((ch = getopt (argc, argv, "s:")) != -1) { + switch (ch) { + case 's': + shift = atoi (optarg) % NR_OF_LETTERS; + break; + } + } + if (shift < 0) { + fprintf (stderr, "Requires an -s parameter\n"); + exit (1); + } + + while ((strlen = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + while (* line_ptr) { + if (isupper (* line_ptr)) { + * line_ptr -= shift; + if (* line_ptr < 'A') { + * line_ptr += NR_OF_LETTERS; + } + } + line_ptr ++; + } + printf ("%s", line); + } + free (line); + return (0); +} |
