이 C++ 프로그램에서 문자열의 공백은 하이픈으로 바뀝니다. 첫째, 문자열의 길이는 cstring 의 length() 함수에 의해 결정됩니다. 클래스에서 다음과 같이 문자열을 순회하여 문장의 공백에 하이픈을 채웁니다.
예시
#include <cstring>
#include <iostream>
using namespace std;
int main(){
// raw string declaration
string str = "Coding in C++ programming";
cout<<"Normal String::"<<str<<endl;
for (int i = 0; i < str.length(); ++i) {
// replacing character to '-' with a 'space'.
if (str[i] == ' ') {
str[i] = '-';
}
}
// output string with '-'.
cout <<"Output string::"<< str << endl;
return 0;
} 출력
사용자가 다음과 같이 문자열을 입력할 때 프로그램의 출력은 다음과 같이 하이픈 조정이 있는 yield입니다.
Normal String::Coding in C++ programming Output string::Coding-in-C++-programming