줄 바꿈하기 전에 계산할 문자열과 문자 양을받는 텍스트 줄 바꿈 함수를 만들려고합니다. 가능하다면 이전 공간을 찾고 포장하는 것으로부터 어떤 단어가 잘리지 않도록하고 싶습니다.C++의 공백에서 텍스트를 줄 바꿈 하시겠습니까?
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
string textWrap(string str, int chars) {
string end = "\n";
int charTotal = str.length();
while (charTotal>0) {
if (str.at(chars) == ' ') {
str.replace(chars, 1, end);
}
else {
str.replace(str.rfind(' ',chars), 1, end);
}
charTotal -= chars;
}
return str;
}
int main()
{
//function call
cout << textWrap("I want to wrap this text after about 15 characters please.", 15);
return 0;
}
기존 코드에 대해 궁금한 점은 무엇입니까? 작동하지 않습니까? 그렇다면 어떻게 실패합니까? – LThode