aboutsummaryrefslogtreecommitdiff
path: root/challenge-180/deadmarshal/cpp/ch-1.cpp
blob: 389fdd96e6aeddba2f644a6ff4a70baecdff831d (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
#include<cstdio>
#include<vector>
#include<string>
#include<unordered_map>

char *first_unique_character(const std::string& str)
{
  char *buffer = new char[39]; 
  std::vector<char> chars{str.begin(), str.end()};
  std::unordered_map<char, unsigned> hash{};
  for(const char& c : str) hash[c]++;
  for(std::size_t i = 0; i < chars.size(); ++i)
    if(hash[chars[i]] == 1)
    {
      std::snprintf(buffer,
		    39,
		    "%zu as '%c' is the first unique character",
		    i,
		    chars[i]);
      break;
    }
  return buffer;
}

int main(int argc, char *argv[])
{
  if(argc < 2)
  {
    fprintf(stderr, "No arg(s) provided!");
    exit(1);
  }
  char *buffer = first_unique_character(argv[1]);
  printf("%s\n", buffer);
  delete buffer;
  
  return 0;
}