aboutsummaryrefslogtreecommitdiff
path: root/challenge-240/paulo-custodio/cpp/ch-1.cpp
blob: 723d518db296be66a68f3fa674bdb365fa4a375b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Challenge 240

Task 1: Acronym
Submitted by: Mohammad S Anwar

You are given an array of strings and a check string.

Write a script to find out if the check string is the acronym of the words in
the given array.
Example 1

Input: @str = ("Perl", "Python", "Pascal")
       $chk = "ppp"
Output: true

Example 2

Input: @str = ("Perl", "Raku")
       $chk = "rp"
Output: false

Example 3

Input: @str = ("Oracle", "Awk", "C")
       $chk = "oac"
Output: true
*/

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void usage(void) {
    cerr << "Usage: ch-1 -str s1 s2 s3 ... -chk check" << endl;
    exit(EXIT_FAILURE);
}

int main(int argc, char* argv[]) {
    vector<string> strs;
    string check, inits;

    // parse args
    int i = 1;
    while (i < argc) {
        string arg = argv[i];
        if (arg=="-str") {
            i++;
            while (i < argc && argv[i][0] != '-') {
                strs.emplace_back(argv[i]);
                i++;
            }
        }
        else if (arg=="-chk") {
            i++;
            if (i < argc) {
                check = argv[i];
                i++;
            }
        }
        else {
            usage();
        }
    }
    if (strs.empty() || check.empty())
        usage();

    // process
    transform(check.begin(), check.end(), check.begin(), ::tolower);

    for (size_t i = 0; i < strs.size(); i++) {
        inits.push_back(tolower(strs[i].front()));
    }

    if (inits==check) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
}