diff options
| -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); +} |
