Function to swap Digits of an integer.

closed account (4y5EwA7f)
For ex.
int swapDigits(int m, int n, int x){}

The function must swap the mth and nth dgits of the number x.
So if x=12345, and m=3, n=2, the output must be: 13245.

I need some help, I don't have any great idea to solve it.

closed account (iGLbpfjN)
Try converting x in a string --> use the function to_string()
Then, swap the elements of the string array. Use this logic:
1
2
3
temporary = string[index_1];
string[index_1] = string[index_2];
string[index_2] = temporary;
closed account (4y5EwA7f)
Sorry that I did not mention, but I must not use arrays and string.

Till now, my code can only get the mth and nth digit value. I don't know how to continue.
closed account (iGLbpfjN)
Then, you'll have to create some mathematical stuff to split the integer into digits. Sorry, but I don't have any ideas... Maybe googling it'll solve your problem =D
It wasn't hard to do mathematically, but it may take a while to digest.

edit: It only works for 12345... The problem seems to be the calculation for d.
edit2: Found the problem. It's up to you to implement it. Hope this helps.
edit3: http://cpp.sh/8dh6


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
int swapDigits( int m, int n, int x ) 
{
    /*
    m = 2, n = 3, x = 12345
    expected result = 13245
    13245 - 12345 = 900
    
    m = 1, n = 3, x = 14793
    expected result = 74193
    74193 - 14793 = 59400
    
    m = 2, n = 5, x = 26937
    expected result = 27936
    27936 - 26937 = 999
    
    in all these, there is a pattern.
    in example 1,
        noOfDigits = 5
        a = noOfDigits - m   b = noOfDigits - n       c = nth digit - mth digit
        x = 3                  = 2                      = 3 - 2
                                                        = 1
        
        (10^a - 10^b) * c = (1000 - 100) * 1
                          = 900
                      
    in example 2,
        a = 4   b = 2   c = 6
        
        (10^4 - 10^2) * 6 = 59400
        
    in example 3,
        a = 3   b = 0   c = 1
        
        (10^3 - 10^0) * 1 = 999
    
    see the pattern?
    */
    
    /*
    add checks:
        m < n < noOfDigits
        check the validity of c, i.e. what if the nth digit is greater than the mth digit?
        whatever else you can think of
    */
    
    int temp{ x }, noOfDigits{ 0 };
    while( temp % 10 ) {
        noOfDigits++;
        temp /= 10;
    }
    
    int mthDigit{ 0 }, nthDigit{ 0 }; 
    /*
    find a way to do this
    mthDigit = 
    nthDigit =
    */
    
    int a{ noOfDigits - m }, b{ noOfDigits - n }, c{ /*fix me*/ };
    
    int d = (pow(10, a) - pow(10, b)) * c;
    
    return x + d;
}
Last edited on
You must determine the position of the nth and mth digits from the beginning of the number.

For instance in 12345, where m and [/tt]n[/tt] are 3 and 2, the mth digit is 2 digits from the ones digit (with a value of 3) and the nth digit is 3 digits from the ones digit (with a value of 2.)

First, you must remove the digits from number, and one can do that by multiplying the digit's value (3 in the case of the mth digit) by 10 to the power of the distance from the ones digit and subtracting that value from the original number.

So, one would subtract 3*10^2 (300) for the mth digit and 2*10^3 (2000) for the nth digit leaving: 10045

Do you see where this is going? What steps need to occur for 10045 to become 13245?
This is my way of doing it. You could add the thousands value and so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>

using namespace std;

int swapDigits(int x, int n, int m);


int main(){

    cout << swapDigits(2, 3, 123) << "\n";

}

int swapDigits(int x, int n, int m){

    int hundreds;
    int tens;
    int ones;

    hundreds = m / 100;

    if(m > 100){

        m = m - (hundreds * 100);

    }

    tens = m / 10;


    if(m > 10){

        m = m - (tens * 10);

    }

    ones = m / 1;

    if(hundreds == x){

        hundreds = n;

    }else if(hundreds == n){

        hundreds = x;

    }

    if(tens == x){

        tens = n;

    }else if(tens == n){

        tens = x;

    }


    if(ones == x){

        ones = n;

    }else if(ones == n){

        ones = x;

    }



    hundreds *= 100;
    tens *= 10;
    ones *= 1;

    m = 0;

    m += hundreds + tens + ones;

    return m;

}
closed account (4y5EwA7f)
@integralfx
that's a nice way to solve it, I'm working on it.
Thanks!
Topic archived. No new replies allowed.