[C++] 생성한 문자열 공백없애는 함수 만들기

2022. 6. 14. 17:30코딩 1막 <C++개념편>/코딩 1막 <C++응용편>

728x90

*문자열 string

#include <iostream>
#include <string>

using namespace std;

string GetStringRemoveSpace(string targetText, string target)
{
    string result;
    while (targetText.find(target) != string::npos)
    {
        result += targetText.substr(0, targetText.find(target));
        targetText = targetText.substr(targetText.find(target) + target.length());
    }
    result += targetText;
    return result;
}

void main()
{

    string targetText = "Hello Hello World World";
    string result = "";


    while (targetText.find(" ") != string::npos)
    {
        result += targetText.substr(0, targetText.find(" "));
        targetText = targetText.substr(targetText.find(" ") + 1);
    }
    result += targetText;
    cout << result << endl;

    result = GetStringRemoveSpace(GetStringRemoveSpace("Hello Hello World World TEST", "Hello")," ");
    cout << result << endl;
}

출력 결과

728x90