1

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");
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Now is a good time to learn how to *debug* your programs. For example with a *debugger* you can catch crashes as and when they happen, and located where in your code they happen, and also examine variables and their values at the time of the crash. – Some programmer dude May 10 '22 at 06:12
  • Also note that the string will have a specific size and capacity when you receive it as argument. Any indexing outside the size will lead to *undefined behavior*. You need to create a *new* string. – Some programmer dude May 10 '22 at 06:14
  • 3
    On a couple of unrelated notes: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) And [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude May 10 '22 at 06:20
  • 1
    On a more related note, please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude May 10 '22 at 06:21
  • @Scheff'sCat: [in place modification is actually shorter and simpler](http://coliru.stacked-crooked.com/a/51ebaa53feaa4150) (though only minutely). This this is still only sort-of in-place (the result is a separate string from the original input). – Jerry Coffin May 10 '22 at 06:46

3 Answers3

2

std::string already has a replace member function, so you can do something like this:

std::string replaceSpaces(std::string input) {
    for (std::size_t i = 0; i < input.length(); i++) {
        if (input[i] == ' ')
            input.replace(i, 1, "@40");
    }
    return input;
}

Although it doesn't arise with this example, there is one situation that can require some care to handle correctly: if the replacement string also contains the string you're trying to replace (e.g, replacing each space with two consecutive spaces) you need to be careful about starting your next iteration of searching just after the end of the replacement value you just inserted -- otherwise, you can end up doing infinite replacements (or at least until you run out of available space).

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

std::string creates a buffer for the size of the string, when you re-size it, you are using more space, and that is overflowing the allocated buffer.

Typically the best pattern for this sort of code, is not to do an in-space modification, but to create a new string which takes the output.

mksteve
  • 12,614
  • 3
  • 28
  • 50
0

you can do following:

#include <bits/stdc++.h>

using namespace std;

string replaceSpaces(string str){
    int n = str.size();
    string tmp;
    for(int i=0; i<n; i++) {
        if(str[i] == ' ') {
            tmp.push_back('@');
            tmp.push_back('4');
            tmp.push_back('0');
        }
        else {
            tmp.push_back(str[i]);
        }
    }
    return tmp;
}

int main() {

    cout << replaceSpaces("My name is Khan");
}
samnoon
  • 1,340
  • 2
  • 13
  • 23