-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alphabet_Counting.cpp
122 lines (96 loc) · 2.53 KB
/
Alphabet_Counting.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// main.cpp
// Interview_1
//
// Created by Nuri Ra on 18.06.18.
// Copyright © 2018 Nuri Ra. All rights reserved.
//
#include <iostream>
int iTokenCnt = 0;
std::string* tokenSentence;
void makeToken(std::string strSentence)
{
int i = 0;
int iLength = strSentence.size();
char charSentence[iLength];
char *token;
strcpy(charSentence, strSentence.c_str());
// Delete special characters, count of all sentence
for(i=0; i<iLength; i++)
{
if(64 < charSentence[i] && charSentence[i] < 91); // 'A' ~ 'Z'
else if(96 < charSentence[i] && charSentence[i] < 123); // 'a' ~ 'z'
else if(charSentence[i] == ' ') // ' '
iTokenCnt++;
else
charSentence[i] = ' ';
}
iTokenCnt = iTokenCnt+1;
// Input of all sentence
i = 0;
tokenSentence = new std::string[iTokenCnt];
token = strtok(charSentence, " ");
while(token != NULL)
{
tokenSentence[i] = token;
i++;
token = strtok(NULL, " ");
}
}
void countToken(std::string* tokenSentence)
{
int iCount;
int i, j;
std::string strStandard;
// Count of removal duplicated value
for(i=0; i<iTokenCnt; i++)
{
if(tokenSentence[i] != "")
{
strStandard = tokenSentence[i];
iCount = 0;
for(j=0; j<iTokenCnt; j++)
{
if(strStandard == tokenSentence[j])
{
tokenSentence[j] = "";
iCount++;
}
}
std::cout << strStandard << " -> " << iCount << "\n";
}
}
}
// Second Solution
void countString(std::string strSnt)
{
int i = 0;
int iCnt[26] = {0, };
unsigned long iLng = strSnt.size();
// Count
for(i=0; i<iLng ; i++)
{
if(64 < strSnt[i] && strSnt[i] < 91) // 'A' ~ 'Z'
iCnt[strSnt[i] - 65]++;
else if(96 < strSnt[i] && strSnt[i] < 123) // 'a' ~ 'z'
iCnt[strSnt[i] - 97]++;
}
// Print
for(i=0; i<27; i++)
{
if(iCnt[i] > 0)
std::cout << char(i+65) << " -> " << iCnt[i] << "\n";
}
}
int main(int argc, const char * argv[]) {
// insert code here...
// std::cout << "Hello, World!\n";
std::string strSnt;
std::cout << "Input : ";
std::cin >> strSnt;
countString(strSnt);
//makeToken(strSentence);
//countToken(tokenSentence);
delete[] tokenSentence;
return 0;
}