diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-09 00:00:49 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-11 21:49:58 +0000 |
| commit | 45b61fa12dcdd1075ef4f23583384c15643c602f (patch) | |
| tree | 11e6b06416d835fea1a5e0d2bc426047167c9758 /challenge-099/paulo-custodio/cpp/ch-1.cpp | |
| parent | 090da47b42eb061cec9f7f395e6aa17fd0b500a1 (diff) | |
| download | perlweeklychallenge-club-45b61fa12dcdd1075ef4f23583384c15643c602f.tar.gz perlweeklychallenge-club-45b61fa12dcdd1075ef4f23583384c15643c602f.tar.bz2 perlweeklychallenge-club-45b61fa12dcdd1075ef4f23583384c15643c602f.zip | |
Add Perl, C, C++ and Basic solutions to challenge 99
Diffstat (limited to 'challenge-099/paulo-custodio/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-099/paulo-custodio/cpp/ch-1.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-099/paulo-custodio/cpp/ch-1.cpp b/challenge-099/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..5fe6d95de2 --- /dev/null +++ b/challenge-099/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,62 @@ +/* +TASK #1 › Pattern Match +Submitted by: Mohammad S Anwar +You are given a string $S and a pattern $P. + +Write a script to check if given pattern validate the entire string. +Print 1 if pass otherwise 0. + +The patterns can also have the following characters: + +? - Match any single character. +* - Match any sequence of characters. +Example 1: +Input: $S = "abcde" $P = "a*e" +Output: 1 +Example 2: +Input: $S = "abcde" $P = "a*d" +Output: 0 +Example 3: +Input: $S = "abcde" $P = "?b*d" +Output: 0 +Example 4: +Input: $S = "abcde" $P = "a*c?e" +Output: 1 +*/ + +#include <iostream> + +bool match(const char* s, const char* p) { + while (true) { + if (!*s && !*p) // string and pattern finished + return true; + else if (!*s || !*p) // either string or pattern finished + return false; + else if (*p == '?') { // match any character + s++; p++; + } + else if (*p == '*') { // match any sub-sequence + p++; + for (int i = 0; s[i]; i++) { + if (match(s + i, p)) + return true; + } + return false; + } + else if (*p != *s) { // chars different + return false; + } + else { // search next char + s++; p++; + } + } +} + +int main(int argc, char* argv[]) { + if (argc == 3) + std::cout << (match(argv[1], argv[2]) ? 1 : 0) << std::endl; + else { + std::cerr << "Usage: ch-1 string pattern" << std::endl; + return EXIT_FAILURE; + } +} |
