-3

I have written a P function that removes c digits in an n integer number.

The head of the function is : void P(int &n, int &c).

Let's say n = 23734 and c = 3
The output should be: 274

This what i have tried before asking here.

void P(int &n,int &c) {

int p=1,k=0;
while(n)
{
    if(n%10==c)
        n=n/10;
        else
        {
            k=k+n%10*10;
            n=n/10;
        }
}
cout<<k;

}

P.S. I am new here that's why i don't know how to correctly add codes.

  • 2
    Easiest way is to convert `n` to a string and `c` to a `char`, then erase the matching chars from the string, then convert the result back to an `int`. If you don't want to involve a string, then this problem can be solved with some simple math instead – Remy Lebeau May 06 '19 at 16:45
  • How to solve with math? – DaRk GameeR_HD May 06 '19 at 16:49
  • 2
    Some hints: `1234 % 10 = 4`. And `1234 / 10 = 123`. Use a loop to check each digit. – 001 May 06 '19 at 16:50
  • 1
    @JohnnyMopp I think you may be misreading the question (I interpreted it the same way at first too). Look closely at `n` vs. the expected output. They want to remove all digits that match `c`. – scohe001 May 06 '19 at 16:51
  • Welcome to [so]. Your question is likely related to homework. While it is OK to ask for homework help on SO, you should at least take some effort to solve the problem yourself, otherwise you don't learn anything. See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic). What you need to do is to try writing the code yourself and, when you get stuck, post a question here showing what you have done and explaining where you are stuck. SO is not a code writing or homework completion service. – Adrian W May 06 '19 at 17:11
  • 2
    As I see it they need to deconstruct n into its individual digits then reconstruct it without c. I would do that in a loop using mod and division. – 001 May 06 '19 at 17:30
  • @JohnnyMopp yes, exactly what I was thinking – Remy Lebeau May 07 '19 at 03:15
  • I've added how I tried to solve this. – DaRk GameeR_HD May 07 '19 at 15:36

1 Answers1

3

Use std::to_string() to convert your n to a string:

std::string s = std::to_string(n);

then convert c to a char by adding 48 to it which is the equivalent of ‘0’ + your number c = some char between ‘0’ and ‘9’.

char myChar = '0' + c;

Now erase every occurrence of myChar by looping through the string and removing the index where myChar == s[i]

for(int i = 0; i < s.size(); i ++){
    If(myChar == s[i]){
        s.erase(s.begin()+i);
        i--;
    }
}

Finally, convert the string back to an int:

n = std::stoi (s);

Edit: you can accomplish this with division and modulo operations as well, as mentioned in one of the comments.

Here's an example I just put together:

bool sign = false; // false -> + , true -> -
if (n < 0) { n = -n; sign = true; }
int back = 0;
int front = n;

int i = 0;
while (front > 0) {
    if(front % 10 != c){
        back += (front % 10) * std::pow(10, i++);
    }
    front /= 10;
}
n = sign ? -back : back;
Renge
  • 502
  • 3
  • 16