이 섹션에서는 C++의 문자열에서 일부 문자를 제거하는 방법을 살펴보겠습니다. C++에서는 이 작업을 erase() 및 remove() 함수를 사용하여 매우 쉽게 수행할 수 있습니다. 제거 함수는 문자열의 시작 및 끝 주소와 제거될 문자를 사용합니다.
Input: A number string “ABAABACCABA” Output: “BBCCB”
알고리즘
Step 1:Take a string Step 2: Remove each occurrence of a specific character using remove() function Step 3: Print the result. Step 4: End
예시 코드
#include<iostream>
#include<algorithm>
using namespace std;
main() {
string my_str = "ABAABACCABA";
cout << "Initial string: " << my_str << endl;
my_str.erase(remove(my_str.begin(), my_str.end(), 'A'), my_str.end()); //remove A from string
cout << "Final string: " << my_str;
} 출력
Initial string: ABAABACCABA Final string: BBCCB