-
Notifications
You must be signed in to change notification settings - Fork 0
/
031_CaptilizeTitle.cpp
69 lines (67 loc) · 1.28 KB
/
031_CaptilizeTitle.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
#include <bits/stdc++.h>
using namespace std;
string capitalize(string s)
{
bool ret = true;
for (int i = 0; i < s.length(); i++)
{
if (ret == true)
{
if (s[i] >= 'a' && s[i] <= 'z')
{
s[i] = s[i] - 'a' + 'A';
}
ret = !ret;
}
else
{
if (s[i] >= 'A' && s[i] <= 'Z')
{
s[i] = s[i] - 'A' + 'a';
}
}
}
return s;
}
string small(string s)
{
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
{
s[i] = s[i] - 'A' + 'a';
}
}
return s;
}
string capitalizeTitle(string s)
{
string newOne;
string ans;
for (int i = 0; i < s.length(); i++)
{
for (; s[i] != ' ' && i < s.length(); i++)
{
newOne.push_back(s[i]);
}
if (newOne.length() >= 3)
{
newOne = capitalize(newOne);
}
else
{
newOne = small(newOne);
}
ans = ans + newOne;
ans = ans + " ";
newOne.clear();
}
ans.pop_back();
return ans;
}
int main()
{
string title = "capiTalIze tHe titLe";
cout << capitalize(title) << endl;
return 0;
}