2019년 4월 13일 토요일

# 주어진 문자열에서 중복되지 않는 문자들을 출력

 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
#include <iostream>
#include <Windows.h>
#include <map>


using namespace std;

void NotRepetition(char* srcString);
void Print(map<char, int>& string_LookUpTable);
void main()
{
 
 char* testChar = "Hello ab"; // 상수 문자열 리터럴. ( only read )
 char testChar2[9] = { 'H', 'e', 'l', 'l', 'o', ' ', 'a', 'b', '\0' };
 char testChar3[10] = { 't', 'o', 't', ' ', 'h', 'e', 'l', 'l','o', '\0' };

 NotRepetition(testChar2);

 system("pause");
}

void NotRepetition(char* srcString)
{
 map<char, int> string_LookUpTable;
 pair<map<char, int>::iterator, bool> insertData;

 int idx = 0;
 while (srcString[idx] != '\0')
 {
  insertData = string_LookUpTable.insert(pair<char, int>(srcString[idx], 1));
  if (insertData.second == false) insertData.first->second += 1;

  idx++;
 }

 Print(string_LookUpTable);
}


void Print(map<char, int>& string_LookUpTable)
{

 map<char, int>::iterator it = string_LookUpTable.begin();
 while (it != string_LookUpTable.end())
 {
  
  if (it->second == 1)
  {
   cout << "문자 : " << it->first << " 등장 횟수 : " << it->second << endl;   
  }

  it++;
 }
}

댓글 없음:

댓글 쓰기