반응형

tolower ,toupper a~z, A~Z 의 범위를 변환 시킨다






http://neodreamer.tistory.com/267



std::string 의 문자열을 대소문자 변환이 필요해 찾아보았는데 쉽게 처리할 수 있는 방법을 찾았다.

std::transform 함와 cctype 헤더의 tolower() 와 toupper() 함수를 이용하면 쉽게 해결이 되었다.

transform 함수는 이외에도 응용할 부분이 많아보인다. container 의 각 원소에 특정을 변형을 주어
바꾸거나 다는 container에 넣을 수도 있다. transform 함수에 대해서는 좀더 공부를 해서 나중에 포스팅을 한번 해야겠다.



#include <cctype> // for toupper & tolower
#include <string>
#include <algorithm>
using namespace std;


	// 대문자로 바꾸기
	string s1 = "sample string";
	transform( s1.begin(), s1.end(), s1.begin(), toupper );
	// 결과 : s1	"SAMPLE STRING"

	// 소문자로 바꾸기
	string s2 = "HELLO";
	transform( s2.begin(), s2.end(), s2.begin(), tolower );
	// 결과 : s2	"hello"


	// 첫 문자만 대문자로 바꾸기
	string s3 = "title";
	transform( s3.begin(), s3.begin() + 1, s3.begin(), toupper );
	// 결과 : s3	"Title"

반응형

+ Recent posts