I have written the code and according to me code should be working but it is not working. It is giving me runtime error. can anybody check my ques my help me with the same?
Here is my code
#include <bits/stdc++.h>
using namespace std;
string replaceSpaces(string str){
int spaces_count = 0;
int length = str.length();
for(int i = 0 ; i < str.length() ; i++){
if(str[i] == ' ' ){
spaces_count++;
}
}
int new_length = length + spaces_count * 2 + 1;
int index = new_length - 1;
str[index] = '\0';
index--;
for(int j = length - 1 ; j >=0 ; j--){
if(str[j] == ' '){
str[index] = '0';
str[index-1] = '4';
str[index-2] = '@';
index = index - 3;
}
else{
str[index] = str[j];
index--;
}
}
return str;
}
int main() {
cout << replaceSpaces("My name is Khan");
}