How do I swap these digits?

This is a discussion on How do I swap these digits? within the C++ Programming forums, part of the General Programming Boards category; This program reads a four-digit integer, and encrypts it. I have had to replace each digit by the sum of ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    How do I swap these digits?

    This program reads a four-digit integer, and encrypts it. I have had to replace each digit by the sum of that digit plus 7 modulus 10. The final step is the swap digit1 with digit3 and swap digit2 with digit4. But I am not quite sure how to do that. Could somebody help me out please?

    Code:
    #include<iostream>
    #include<conio>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       int Integer;
       int Divider;
       int Digit1, Digit2, Digit3, Digit4;
    
       cout << "Enter a four-digit integer: ";
       cin >> Integer;
    
       Divider = 1000;
       Digit1 = Integer / Divider;
    
       Integer = Integer % Divider;
       Divider = 100;
       Digit2  = Integer / Divider;
    
       Integer = Integer % Divider;
       Divider = 10;
       Digit3  = Integer / Divider;
    
       Integer = Integer % Divider;
       Divider = 1;
       Digit4  = Integer / Divider;
    
          Digit1 = (Digit1 + 7) % 10;
          Digit2 = (Digit2 + 7) % 10;
          Digit3 = (Digit3 + 7) % 10;
          Digit4 = (Digit4 + 7) % 10;
    
       cout << Digit1 << Digit2 << Digit3 << Digit4;
    
       getch();
       return 0;
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Move digit1 to temp value
    move digit3 to digit1
    move temp to digit3
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    may not even have to perform the swap:
    cout << Digit3 << Digit4 << Digit1 << Digit2;
    could also suffice, if the program ends there

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    15
    std::swap?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 05:32 AM
  3. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  4. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 09:39 PM
  5. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 05:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21