-3

How would you be able to replace digits in a given number using basic c++. Example if the number is 23444 and you want to take the old digit 4 and replace is with a new digit 5 to get a new number 23555. I have some work done below but when I enter the inputs, it ends up giving me an incorrect result.

    cout << "Enter the number: " << endl;
    cin >> number;
    cout << "Enter the old digit: " << endl;
    cin >> oldDigit;
    cout << "Enter the newDigit: " << endl;
    cin >> newDigit;

    newDigit=oldDigit;

    cout << Newnum << endl;
Maria
  • 1
  • 1
  • 6
  • yes please add your current code and explain how it doesn't work as expected. – Paul Rooney Sep 28 '16 at 05:37
  • 1
    It's best to post a [mcve]. – R Sahu Sep 28 '16 at 05:38
  • You can convert int to string using itoa() and iterate over it to check does it contain number. If it does, get 4's position and replace it with 5. I'll post answer soon – Kamila Szewczyk Sep 28 '16 at 05:40
  • added and no strings allowed for this program, we have not gotten to working with strings yet – Maria Sep 28 '16 at 05:43
  • You didn't give us the body of `replaceDig`. But from what I can see, you are not using the return value at all. In C, arguments are copies by default. Also, @Joel, this shouldn't be a hard problem. Just iterate over the digits like was done, and use some additions, soustractions, and multiplications. – MayeulC Sep 28 '16 at 05:48
  • I wasn't clear on how to proceed with the body of replaceDig. @MayeulC – Maria Sep 28 '16 at 05:54
  • 3
    Lex, nuking a question is very uncool. – user4581301 Sep 28 '16 at 06:15
  • @lex925 you don't need to clean up your code (and still wrong) after there are some answer available. Besides that, base on your previous question, you have a XY problem here. – apple apple Sep 28 '16 at 06:37

6 Answers6

2

You can convert int to char* using itoa() and iterate over it to check does it contain number. If it does, get 4's position and replace it with 5.

I know you didnt work with strings, but it can be helpful in your case. Simple code:

#include <string.h>
#include <iostream>
#include <stdlib.h>
int main(){
  int numer;
  std::cin>>numer;
  char* str;
  itoa(numer, str, 10);
  for(int i = 0; i < strlen(str); i++){
    if(str[i] == '4') str[i]='5';
  }
}
Kamila Szewczyk
  • 1,874
  • 1
  • 16
  • 33
1

If I understand you correctly, you don't just want to simply add 111, you want to treat the number as a string, then change elements in the array. Is that correct?

This may get you on the right track:

Convert an int to ASCII character

Community
  • 1
  • 1
Andy Alt
  • 297
  • 2
  • 12
1

if you really want to use only int to do this, here is a working example (base on some of your code)

#include <iostream>
using namespace std;

int replaceDig( int num, int oldDigit, int newDigit)
{
   if(num==0)return 0;

   int digit = num%10;
   if(digit==oldDigit)digit = newDigit;

   return replaceDig(num/10,oldDigit,newDigit)*10+digit;
}

int main()
{
    int num, newnum, oldDigit, newDigit;
    cout << "Enter the number: " << endl;
    cin >> num;
    cout << "Enter the old digit: " << endl;
    cin >> oldDigit;
    cout << "Enter the newDigit: " << endl;
    cin >> newDigit;

    newnum = replaceDig(num, oldDigit, newDigit);
    cout << newnum << endl;
    return newnum; //do you really want to return this?
}
apple apple
  • 10,292
  • 2
  • 16
  • 36
0

I have come up with a solution . Don't know if it contains bug or not. Please let me know.

int num = 23444 ,new_num = 0;
int mod;
int exponent = 0; 


/**
 * Now form the new number
 */

while ( num > 0 ) {
    mod = num % 10;
    num /= 10;
    if ( mod == 4 ) // check whether this is the old digit or not
        new_num += 5 * pow( 10 , exp); // replace with new digit
    else
        new_num += mod * pow(10 , exp); // otherwise no change

    exp++;
}
num = new_num;
std::cout << num;
Parnab Sanyal
  • 749
  • 5
  • 19
0

I hope this works for you -

std::string s = std::to_string(23444);
std::replace( s.begin(), s.end(), '4', '5');
int num = std::stoi(s);
adisticated
  • 469
  • 2
  • 10
0
int replaceDig( int num, int oldDigit, int newDigit) // replacing the old digits in a number with a new digit
{
    int position = numDigits(num);
    int remainder = num;
    int currentDigit;
    while (remainder >0)
    {
        currentDigit=(num/pow(10,position))%10;
        if(currentDigit==oldDigit)
        {
            num = num - oldDigit*pow(10,position);
            num = num + newDigit*pow(10,position);
        }
        remainder = remainder/10;
        position--;
    }
}

This is the general idea, I guess. I didn't try to compile it though. And of course, this version isn't really optimized and we could find some more efficient ways of doing it. Oh, and it doesn't work with negative numbers, but this should be quite easy to adapt.

MayeulC
  • 1,628
  • 17
  • 24
  • why `position = position--` ? It is an undefined behaviour. – Parnab Sanyal Sep 28 '16 at 06:07
  • Because typo :) Fixed, thanks (btw, your solution is quite similar, I believe. But I didn't receive a new answer notification). – MayeulC Sep 28 '16 at 06:07
  • 1
    Future note: `pow` is nice and readable here (as in don't change it) but it can be a pretty expensive operation when you can have a variable keep track of the place with a single multiply. Eg. `tens *= 10;` – user4581301 Sep 28 '16 at 06:12