노트

c++) 문자열 관련 함수들 저장

tose33 2021. 4. 2. 22:47

입력관련:

cin

<iostream>

표준 입력 버퍼에서 개행 문자 제외한 값 가져옴.

공백, 개행 입력시 그 전까지 받아들임.

개행 문자를 입력 버퍼에 남겨둠.

 

cin.getline

<iostream>

cin.getline(변수 주소, 최대 입력 문자수, 종결 문자)

char 형 배열에 저장가능.

ex) cin.getline(str, 10)

 

null 관련 참고: m.blog.naver.com/PostView.nhn?blogId=ekthatkxkd&logNo=221098511549&proxyReferer=https:%2F%2Fwww.google.co.kr%2F

 

getline

string 라이브러리에 포함.

string에 저장가능.

ex) getline(cin, string)

 

int tolower(int c);

소문자로 변경

 

int toupper(int c);

대문자로 변경


reverse

문자열 거꾸로 변환

ex) reverse(str.begin(), str.end());

 

문자열 변환함수

stoi : string to int

stof : string to float

stol : string to long int

stod : string to double

 

ex) int num = stoi(str1);

 


substr

문자열의 일부를 리턴

substr(pos, count)

 

pos: 첫번째 문자의 위치 (원래의 문자열에서)

count: 부분 문자열의 길이

 

리턴: 원래 문자열에서 [pos, pos+count) 까지의 문자열 반환.

 

append

string str1 = "abc";

string str2 = "def";

str1.append(str2);

cout << str1;  

// 출력: "abcdef"

 

to_string

Integer을 string으로 변환

 

int num = 10;

cout << to_string(num); 

// 출력: "10"

 

string.find

찾는 문자열을 찾았다면 해당 문자열의 시작지점 반환.

찾지 못했다면 string::npos 반환.

(string::npos is defined with a value of -1) 

www.cplusplus.com/reference/string/string/npos/

 

ex) 

string s = "abc";

if(s.find("d") == -1) cout << "no d";

 

output: "no d"