- Input – 1.1.1.1 Output – 1[.]1[.]1[.]1[.]
string ipAddr(string address) {
string result;
for (int i = 0; i< address.length(); i++){
if(address[i] == '.'){
result.append("[.]");
}
else{
result += address[i];
}
}
return result;
}
2. Reverse Words in a String – Input [the sky is blue], Output – [blue is sky the]
string reverseWords(string s) {
// Reverse the whole string
reverse(s, 0, s.length()-1);
int st = 0;
// Reverse each word
while(st <= s.length()){
int word_st = st;
while(s[st] != ' ' && st < s.length()){
st++;
}
int word_en = st - 1;
reverse(s, word_st, word_en);
st++;
}
return s;
}
void reverse(string &s, int st, int en){
int length = en - st + 1;
while(st<= en){
char temp = s[en];
s[en] = s[st];
s[st] = temp;
st++;
en--;
}