-1

I was trying to remove X from a given string, the code compiles and runs but there is no output shown. I guess the problem is where I have to use the 'cout' operator.

Here is my Code:

#include<iostream>
#include<bits/stdc++.h>
#include<string.h>

using namespace std;

void removeX(char str[])
{
    if(str[0]='\0')
    {
        cout<<str;
    }

    if(str[0]!='x'||str[0]!='X')
    {
         removeX(str+1);
    }
    else
    {
        for(int i=0;i!='\0';i++)
        {
            str[i]=str[i+1];
        }
        removeX(str+1);
    }
}

int main()
{
    char a[]="MALCOLM X";
    removeX(a);
    return 0;
}
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 3
    Begin by using `std::string` for all your strings. Then [find](https://en.cppreference.com/w/cpp/algorithm/find) and [erase](https://en.cppreference.com/w/cpp/string/basic_string/erase) the character? – Some programmer dude Jun 11 '21 at 06:31
  • 4
    On another note, please read [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 Jun 11 '21 at 06:33
  • 1
    By the way, `std::cout` is *not* an operator, it's a *stream*... – Aconcagua Jun 11 '21 at 06:45
  • 1
    `if(str[0]='\0')` Look at this, what do you think this is doing? – n. m. could be an AI Jun 11 '21 at 06:45
  • From a point of design: In general you shouldn't let your modifying function print the output, too. This reduces reusability pretty much. Better do outputting elsewhere, in your case within `main` function. – Aconcagua Jun 11 '21 at 06:48
  • Your implementation is pretty inefficient, though. If you have more than one `x` or `X` you move contents towards front multiple times – for the same reason you shouldn't `erase` multiple elements in a `std::vector` each on its own. Better: `for(char* tmp = str; *tmp; ++tmp) { if(*tmp != 'x' && *tmp != 'X') { *str++ = *tmp; } *str = 0;` (note: pointers instead of indices – if you prefer the latter, try to re-implement with them on your own). – Aconcagua Jun 11 '21 at 06:54

2 Answers2

2

You have a few issues in your code, the ones I can spot right away are:

if(str[0]='\0') - this is an assignment, not a comparison. Your entire string will be replaced with \0-characters - no characters will be skipped because this:

if(str[0]!='x'||str[0]!='X') is always true. Ask yourself if x is different from x (false) OR X (true). False or true = true.

The check should be implemented something like (str[0] != 'x' && str[0] != 'X').

Edit: One more issue.

The overall logic will not work (-ish). The part where you run through the string and compress it is correct enough. But you try to run to the end and then print the string, however at that point you are only holding the end of the string (the null termination), so no matter what else you have done you will only print that (aka. print nothing).

Instead, once you are done compressing the xs out of the string, you need to return to the beginning of the string and then print that. The easy way is to print in main, or you can split your function into an outer+inner function like:

void outer_removeX(str) {
  removeX(str);
  print(str);
}

Or you could add an extra variable to the recursion that allows you to return to the first call in the chain and then print there.

However, only printing the end will not work.

Frodyne
  • 3,547
  • 6
  • 16
  • From a point of design it is usually not a good idea to print out from within a modifying function anyway, that reduces reusability pretty much. Rather output at the same level where you trigger modification, in this case `main`. `outer_removeX` as a pure helper function to avoid code repetition is fine as long as the `removeX` function remains available in parallel (i. e. not made `static` in the implementation file or added to anonymous namespace). – Aconcagua Jun 11 '21 at 07:03
0

try the following code:

#include<iostream>
#include<bits/stdc++.h>
#include<string.h>

using namespace std;

void removeX(char str[],int j)
{
    if(str[j]=='\0')
    {
        cout<<str;
    }
     else if(str[j]!='x' && str[j]!='X')
    {
         removeX(str,++j);
    }
    else
    {
        
        for(int i=j;str[i]!='\0';i++)
        {
            str[i]=str[i+1];
        }
        removeX(str,j);
    }
}

int main()
{
    char a[]="MAxLCOLM X";
    removeX(a,0);
    return 0;
}