문자열 자르기

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
#define _CRT_SECURE_NO_WARNINGS
#define INF 187654321
 
 
#include<iostream>
#include <vector>
#include <string>
 
using namespace std;
 
 
vector<string> split(string str, string div) {
 
    vector<string> v;
    int start = 0
    int d = str.find(div);
 
    while (d != -1) {
 
        v.push_back(str.substr(start, d-start));
        start = d + 1;
        d = str.find(div, start);
 
    }
    v.push_back(str.substr(start));
 
    return v;
 
}
 
 
int main() {
 
    vector<string> temp = split("goodbye my friend"" ");
    
    return 0;
}
 
cs

 

문자열로 된 시간 -> 자르기 및 (초 단위)로 변환

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int string_to_sec(string time){
    
    int sec = 0;
    string sub = "";
    
    for(int i=0;i<time.size();i++){
        
        if(time[i] == ':'){
            sec = (sec * 60+ (stoi(sub) * 60);
            sub = "";
            continue;
        }
        sub.push_back(time[i]);
    }
    sec += stoi(sub);
    return sec;
}
 
cs

 

대문자 <-> 소문자

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
#define _CRT_SECURE_NO_WARNINGS
#define INF 187654321
 
 
#include<iostream>
#include <string>
 
using namespace std;
 
 
int main() {
 
    string str = "GOODBYE! my friend";
 
    for (int i = 0; i < str.size(); i++) {
        str[i] = toupper(str[i]);
    }
 
    cout << str << endl;
 
    for (int i = 0; i < str.size(); i++) {
        str[i] = tolower(str[i]);
    }
 
    cout << str << endl;
 
    return 0;
}
 
cs

 

문자 <-> 숫자

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
#define _CRT_SECURE_NO_WARNINGS
#define INF 187654321
 
 
#include<iostream>
#include <string>
 
using namespace std;
 
 
int main() {
 
    string str = "123";
 
    int num = stoi(str); // 문자열 -> 숫자
 
    cout << num + 321 << endl;
 
    string str2 = to_string(num); // 숫자 -> 문자열
    
    str2 += " convert";
 
    cout << str2 << endl;
 
    int sum = 0;
    char ch = '1';
 
    sum += ch - '0'// 문자 -> 숫자
 
    cout << sum << endl;
 
 
    return 0;
}
 
cs

 

문자열 비교 및 문자열 중간 삭제

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
#define _CRT_SECURE_NO_WARNINGS
#define INF 187654321
 
 
#include<iostream>
#include <string>
 
using namespace std;
 
 
int main() {
 
    string str1 = "sam";
    string str2 = "sam";
    string str3 = "mike";
 
    if (str1 == str2) cout << true << endl;
    else cout << false << endl;
 
    if (str1 == str3) cout << true << endl;
    else cout << false << endl;
 
 
    string str4 = "elephant";
 
    str4.erase(23); // 2번 인덱스 부터 3개 삭제
 
    cout << str4 << endl;
 
    return 0;
}
cs

 

 

 

 

 

 

반응형

'자료구조 + 알고리즘' 카테고리의 다른 글

(C++) set container  (0) 2021.09.11
(C++) map container  (0) 2021.09.11
Brute-force Search  (0) 2021.09.05
알고리즘 기초개념, 빅오표기법  (0) 2021.09.05
알고리즘 공부순서  (0) 2021.09.05

+ Recent posts