문자열자르기2 [c++] 문자열을 특정 문자 기준으로 자르기2 getline & stringstream 정석은 아니지만... getline을 사용하면 문자열을 손쉽게 자를 수 있다. getline(&istream is, &string str, char delim) - is는 cin이나 stringstream - str은 자른 문자열 - delim은 기준점이다. 원래 정의대로 라면 delim 문자를 만나면 문자열 추출이 중단된다. 이걸 이용해서 tokenize해보자. 일단 is 자리에 들어갈 stringstream이 필요하다. #include #include #include using namespace std; int main(void){ string s = "This,is,the,test!"; char delim = ' '; stringstream ss(input); string buffer; while(.. 2020. 10. 10. [c c++] 문자열을 특정 문자 기준으로 자르기1 strtok char str[10]; str = "hello i am" char *p = strtok(str, " ") //공백을 기준으로 자른다. 근데 한번만 잘라준다. strtok의 작동방식 문자열을 보고 '특정 문자'가 나오면 특정문자를 '\0'으로 바꿔준다. 그리고 p는 str부터 \0까지의 문자열을 저장 이게 한번만 반복되기 때문에 여러번 하려면 #include char str[10]; 문자열 저장 char *str[10]; 문자열 리스트 저장 str = "hello i am" char *p = strtok(str, " ") while(ptr != null) //마지막 문자열까지 { strcpy(str[i++], p); //문자열 복사 p = strtok(null, " "); } 이렇게 된다. 근데 c++의.. 2020. 4. 11. 이전 1 다음