ในโปรแกรม C++ นี้ ช่องว่างในสตริงจะถูกแทนที่ด้วยยัติภังค์ ประการแรก ความยาวของสตริงถูกกำหนดโดยฟังก์ชัน length() ของ cstring class แล้วเติมยัติภังค์ลงในช่องว่างของประโยคโดยข้ามผ่านสตริงดังนี้
ตัวอย่าง
#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;
} ผลลัพธ์
ผลลัพธ์ของโปรแกรมจะออกมาโดยมียัติภังค์บิดดังต่อไปนี้เมื่อผู้ใช้ป้อนสตริงดังต่อไปนี้
Normal String::Coding in C++ programming Output string::Coding-in-C++-programming