diff options
Diffstat (limited to 'challenge-110')
| -rw-r--r-- | challenge-110/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-110/abigail/c/ch-1.c | 79 |
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-110/abigail/README.md b/challenge-110/abigail/README.md index 051aef0d33..642f4d5bbf 100644 --- a/challenge-110/abigail/README.md +++ b/challenge-110/abigail/README.md @@ -43,6 +43,7 @@ can completly ignore any white space in the input. ### Solutions [AWK](awk/ch-1.awk) [Bash](bash/ch-1.sh) +[C](c/ch-1.c) [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-110/abigail/c/ch-1.c b/challenge-110/abigail/c/ch-1.c new file mode 100644 index 0000000000..011ef2ee99 --- /dev/null +++ b/challenge-110/abigail/c/ch-1.c @@ -0,0 +1,79 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <ctype.h> +# include <stdbool.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * raw = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&raw, &len, stdin)) != -1) { + /* + * Make a copy of line, but without the spaces + */ + char * line; + if ((line = (char *) malloc ((str_len + 2) * sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + char * raw_ptr = raw; + char * line_ptr = line; + while (* raw_ptr) { + /* + * Skip white space + */ + if (isspace (* raw_ptr)) { + raw_ptr ++; + continue; + } + + /* + * If the first character is a '+', write two 0s. + */ + if (line_ptr == line && * raw_ptr == '+') { + * line_ptr ++ = '0'; + * line_ptr ++ = '0'; + raw_ptr ++; + continue; + } + * line_ptr ++ = * raw_ptr ++; + } + * line_ptr = '\0'; + /* + * If the first character is a '(', and the fourth is a ')', + * replace both of them with 0 + */ + if (line [0] == '(' && line [3] == ')') { + line [0] = '0'; + line [3] = '0'; + } + + /* + * Check that we have exactly 14 digits + */ + bool valid = true; + for (size_t i = 0; i < 14 && valid; i ++) { + if (!isdigit (line [i])) { + valid = false; + } + } + if (valid && line [14] == '\0') { + printf ("%s", raw); + } + + free (line); + } + free (raw); + + return (0); +} |
