별의 공부 블로그 🧑🏻‍💻
728x90
728x170

공백 없애기




입력된 문자열에서 공백을 제거하여 출력할 때 <string> 헤더에 있는 erase() 함수를 사용하면 된다.

위의 문제의 답은 아래와 같다.


sequence (1)
 string& erase (size_t pos = 0, size_t len = npos);


pos
Position of the first character to be erased.
If this is greater than the string length, it throws out_of_range.
Note: The first character in str is denoted by a value of 0 (not 1).
len
Number of characters to erase (if the string is shorter, as many characters as possible are erased).
A value of string::npos indicates all characters until the end of the string.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string a;
    
    getline(cin, a);
    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] == ' ') a.erase(i, 1);
    }
    
    cout << a << endl;
    
    return 0;
}
 



728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖