#1. ๋ฐ์ดํฐ ํ์์ ๊ตฌ๋ถ
- string:
1) ์ฌ์ฉํ๊ธฐ: string str = ""; (#include <string> ํค๋๋ฅผ ์จ์ค์ผ ์ฌ์ฉ๊ฐ๋ฅํ๋ค. "" ํฐ ๋ฐ์ดํ ์ฌ์ฉ!)
2) ์ ๋ ฅ๋ฐ๊ธฐ: cin>>str; ๋ก ํ๊ณ ๊ณต๋ฐฑ์ด ํฌํจ๋ ๋ฌธ์์ด์ด ์ ์ฅ๋๋ค.
3) ํฌ๊ธฐ ๊ตฌํ๊ธฐ: str.size() ํน์ str.length(); ๋ก ๊ตฌํ ์ ์๋ค.
4) ์ถ๋ ฅ: cout << str; ํน์ cout << str[i]๋ก ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํด ํ ๋ฌธ์์ฉ ์ถ๋ ฅํ ์๋ ์๋ค.
- char:
1) ์ฌ์ฉํ๊ธฐ: char charater[10]; ์ด๋ ๊ฒ ๋ฐฐ์ดํ์์ผ๋ก ์จ์ผ string ํ์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค. ('' ์์ ๋ฐ์ดํ ์ฌ์ฉ!)
2) ์ ๋ ฅ๋ฐ๊ธฐ: cin>>charater; ํ๋ฉด ํ๊ธ์์ฉ charํ ๋ฐฐ์ด์ ์ ์ฅ๋๋ค. ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ณต๋ฐฑ ์ ๊น์ง ์ ๋ ฅ๋ฐ์ ์๋ ์๋ค.
3) ํฌ๊ธฐ ๊ตฌํ๊ธฐ: strlen(character); ๋ก ๊ตฌํ ์ ์๋ค.
4) ์ถ๋ ฅ: cout << character;๋ก ํ๋ฒ์ ์ถ๋ ฅ. ํน์ cout << character[i]๋ก ํ ๋ฌธ์์ฉ ์ถ๋ ฅ ๊ฐ๋ฅํ๋ค.
#2. string ์ ์ฉํจ์ ์ ๋ฆฌ
- tolower() : ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ณํํ๊ธฐ
1) ํ์คํ์: int tolower ( int c );
2) ์ฌ์ฉ์์ :
/* tolower example */
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main ()
{
string str="Test String";
for(int i = 0; i<str.length(); i++){
str[i] = tolower(str[i]);
cout<<str[i];
}
return 0;
}
- push_back(): string ๋ฌธ์ ํ๋์ฉ ์ถ๊ฐํ๊ธฐ.(๋ค์ ์ฝ์ )
1) ํ์คํ์: void push_back (char c);
2) ์ฌ์ฉ์์ :
// string::push_back
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "ABC";
str.push_back('D');
cout << str << '\n'; // "ABCD"
return 0;
}
- substr(): ํน์ ๋ฌธ์์ด ์ถ์ถ
1) ํ์คํ์: string substr (size_t pos = 0, size_t len = npos) const;
2) ์ฌ์ฉ์์:
// string::substr
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="We think in generalities, but we live in details.";
string str2 = str.substr (3,5); // "think"
size_t pos = str.find("live"); // position of "live" in str
string str3 = str.substr (pos); // get from "live" to the end
cout << str2 << ' ' << str3 << '\n';// "think live in details."
return 0;
}
# C++ ๋ ํผ๋ฐ์ค string
string - C++ Reference (cplusplus.com)
#์ฐธ๊ณ ๋ฌธ์
1) ํ๋ก๊ทธ๋๋จธ์ค - ์ ๊ท ์์ด๋ ์ถ์ฒ https://programmers.co.kr/learn/courses/30/lessons/72410
์ ๋ต์ฝ๋๋ ๋๋ณด๊ธฐ ํด๋ฆญ
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
int main(){
string id = "";
cin >> id;
cout << solution(id);
}
string solution(string id) {
string new_id = "";
//1๋จ๊ณ: ๋๋ฌธ์->์๋ฌธ์๋ก ๋ณํ
for(int i = 0; i<id.size(); i++){
id[i] = tolower(id[i]);
}
//2๋จ๊ณ: ~!@#$%^&*()=+[{]}:?,<>/ ๋ฌธ์ ์ ๊ฑฐ (ํด๋น๋๋ ๋ฌธ์๋ง new_id[]์ ๋ฃ๊ธฐ)
for(int i = 0; i<id.size(); i++){
if(id[i] != '-' && id[i] != '.'
&& !('a'<= id[i] && id[i] <='z')
&& !('0'<=id[i] && id[i]<='9')){
continue;
}
new_id.push_back(id[i]);
}
//3๋จ๊ณ: ๋ง์นจํ(.)๊ฐ 2๋ฒ ์ด์ ์ฐ์๋ ๋ถ๋ถ์ ํ๋์ ๋ง์นจํ(.)๋ก ์นํ
string temp = "";
for(int i = 0; i < new_id.size(); i++){
if(new_id[i] == '.'){
temp.push_back('.');
while(i<new_id.size() && new_id[i] == '.') i++;
i--;
}
else temp.push_back(new_id[i]);
}
new_id = temp;
//4๋จ๊ณ: ์๋์ ์๋ ๋ง์นจํ(.) ์ ๊ฑฐ
if(new_id[0]=='.') new_id = new_id.substr(1);
if(new_id[new_id.size()-1]=='.') new_id = new_id.substr(0, new_id.size()-1);
//5๋จ๊ณ: id๊ฐ ๋น๋ฌธ์์ด์ผ ๊ฒฝ์ฐ, "a"๋ฅผ ๋์
if(new_id.empty()) new_id="a";
//6๋จ๊ณ: id ๊ธธ์ด๊ฐ 16์ ์ด์์ด๋ฉด, 15๊ฐ ์ดํ ๋ฌธ์ ๋ชจ๋ ์ ๊ฑฐ
if(new_id.size()>=16) new_id = new_id.substr(0,15);
if(new_id[new_id.size()-1]=='.') new_id = new_id.substr(0, new_id.size()-1);
//7๋จ๊ณ: id ๊ธธ์ด๊ฐ 2์ ์ดํ๋ผ๋ฉด, id ๋ง์ง๋ง ๋ฌธ์๋ฅผ id์ ๊ธธ์ด๊ฐ 3์ด ๋ ๋๊น์ง ๋ฐ๋ณตํด์ ๋์ ๋ถ์ด๊ธฐ.
while(new_id.size()<=2) new_id += new_id[new_id.size()-1];
return new_id;
}
2) ํ๋ก๊ทธ๋๋จธ์ค - ๊ดํธ ํ์ ํ๊ธฐ https://programmers.co.kr/learn/courses/30/lessons/76502
์ ๋ต์ฝ๋๋ ๋๋ณด๊ธฐ ํด๋ฆญ
#include <string>
#include <stack>
using namespace std;
int findCorrectBracket(string s) {
int correctBracketNum = 0;
stack<char> s_openBracket;
for (int idx = 0; idx < s.length(); idx++) {
char closedBracket;
char currentBracket = s[idx];
//์ด๋ฆฐ๊ดํธ
if (currentBracket == '(' || currentBracket == '{' || currentBracket == '[') {
s_openBracket.push(currentBracket);
}
//๋ซํ๊ดํธ
else {
if (s_openBracket.empty()) {
return 0;
}
switch (currentBracket) {
case ')':
if (s_openBracket.top() != '(') {
return 0;
}
break;
case '}':
if (s_openBracket.top() != '{') {
return 0;
}
break;
case ']':
if (s_openBracket.top() != '[') {
return 0;
}
break;
}
s_openBracket.pop();
}
}
if (s_openBracket.empty()) return 1;
else return 0;
}
string rotateString(string s) {
string firstString = s.substr(0, 1);
string subString = s.substr(1, s.length() - 1);
return subString + firstString;
}
int solution(string s) {
int result = 0;
for (int idx = 0; idx < s.length(); idx++) {
result += findCorrectBracket(s);
s = rotateString(s);
}
return result;
}
'๐ Coding Test Study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๋ฉ ํ ์คํธ ๋๋น] ์ฝํ ์๊ฐ๋ณต์ก๋ ๊ณ์ฐ (0) | 2021.09.21 |
---|---|
[C++][๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ] ๊ตฌํ ๋ฐฉ๋ฒ (0) | 2021.07.29 |
DP(Dynamic Programming)? (0) | 2021.04.27 |
[Algorithm&์๋ฃ๊ตฌ์กฐ] BFS/DFS? (0) | 2021.04.13 |
[์๋ฃ๊ตฌ์กฐ&์๊ณ ๋ฆฌ์ฆ] ๊ทธ๋ํ? (0) | 2021.04.13 |