diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-05-26 17:48:03 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-05-26 17:48:03 +0100 |
| commit | 1be3f1fda107e92c70145da4d3720316e99b8038 (patch) | |
| tree | 1d344e7640343c2122770d17e003d544dad07081 | |
| parent | 6bb0d6c5d3bc8f6adc2f3eaeee4cf60689e2d234 (diff) | |
| download | perlweeklychallenge-club-1be3f1fda107e92c70145da4d3720316e99b8038.tar.gz perlweeklychallenge-club-1be3f1fda107e92c70145da4d3720316e99b8038.tar.bz2 perlweeklychallenge-club-1be3f1fda107e92c70145da4d3720316e99b8038.zip | |
Add C solution
| -rw-r--r-- | challenge-026/paulo-custodio/c/ch-1.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-026/paulo-custodio/c/ch-1.c b/challenge-026/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..26e85e147a --- /dev/null +++ b/challenge-026/paulo-custodio/c/ch-1.c @@ -0,0 +1,33 @@ +/* +Challenge 026 + +Task #1 +Create a script that accepts two strings, let us call it, "stones" and +"jewels". It should print the count of "alphabet" from the string "stones" +found in the string "jewels". For example, if your stones is "chancellor" and +"jewels" is "chocolate", then the script should print "8". To keep it simple, +only A-Z,a-z characters are acceptable. Also make the comparison case +sensitive. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int count_alphabet(const char* stones, const char* jewels) { + int count = 0; + for (const char* p = stones; *p; p++) { + if (strchr(jewels, *p)) + count++; + } + return count; +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + fputs("Usage: ch-1 stones jewels\n", stderr); + return EXIT_FAILURE; + } + + printf("%d\n", count_alphabet(argv[1], argv[2])); +} |
