summaryrefslogtreecommitdiff
path: root/unique-char.cpp
blob: 05e0883493d8051a7fd864282b6b4a7bb184a8ee (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

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <exception>
#include <vector>

using namespace std;

bool is_unique(const char* p, size_t n)
{
    // ASCII range is 32-126
    vector<bool> counts(95, false);
    for (size_t i = 0; i < n; ++i, ++p)
    {
        char c = *p;

        cout << c << endl;

        if (c < 32)
            throw exception();

        size_t pos = c - 32;

        if (pos >= 95)
            throw exception();

        if (counts[pos])
            return false;

        counts[pos] = true;
    }

    return true;
}

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        cout << "needs at least one argument" << endl;
        return EXIT_FAILURE;
    }

    cout << "input string: " << argv[1] << endl;

    size_t n = strlen(argv[1]);
    const char* p = argv[1];

    try
    {
        cout << "all characters are unique: " << (is_unique(p, n) ? "yes" : "no") << endl;
    }
    catch (...)
    {
        cout << "failed to parse the string" << endl;
    }

    return EXIT_SUCCESS;
}