Program to replace every space in a string with hyphen | C ++ program to replace space with - ; Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int main ( ) { string str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( str [ i ] == ' ▁ ' ) { str [ i ] = ' - ' ; } } cout << str << endl ; return 0 ; }