summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2012-07-08 13:27:51 -0400
committerKohei Yoshida <kohei.yoshida@gmail.com>2012-07-08 13:27:51 -0400
commitb0915f392e111b5810736b0822fd16e7f96fff22 (patch)
treeb881c21b9333298720785da5b357075c0f914be9
parent7c3cea8b2df86b36eb95c62d01bfcd23199983d5 (diff)
Completed.
-rw-r--r--unique-char.cpp32
1 files changed, 28 insertions, 4 deletions
diff --git a/unique-char.cpp b/unique-char.cpp
index b8120b2..05e0883 100644
--- a/unique-char.cpp
+++ b/unique-char.cpp
@@ -2,17 +2,33 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
+#include <exception>
+#include <vector>
using namespace std;
bool is_unique(const char* p, size_t n)
{
- static const char offset = 'a';
- size_t counts[26];
+ // ASCII range is 32-126
+ vector<bool> counts(95, false);
for (size_t i = 0; i < n; ++i, ++p)
{
char c = *p;
- cout << *p << endl;
+
+ 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;
@@ -30,7 +46,15 @@ int main(int argc, char** argv)
size_t n = strlen(argv[1]);
const char* p = argv[1];
- cout << "all characters are unique: " << (is_unique(p, n) ? "yes" : "no") << endl;
+
+ try
+ {
+ cout << "all characters are unique: " << (is_unique(p, n) ? "yes" : "no") << endl;
+ }
+ catch (...)
+ {
+ cout << "failed to parse the string" << endl;
+ }
return EXIT_SUCCESS;
}