Advertisement
Checosnake

Twitter Challenge 2 3/4

Jan 19th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. ////////////////////////////////////////
  2. //Code Written By: Sergio A. Nuñez    //
  3. //Date of Final Revision: 1/20/2017   //
  4. ////////////////////////////////////////////////////////////////////////
  5. //Summary of Code:                                                    //
  6. //This program will take an input form the system and decide whether  //
  7. //the input is an email or a phone number then it will do two things  //
  8. //------------------------------------------------------------------- //
  9. //1:                                                                  //
  10. //Email: If an email is detected it will find the '@' symbol then     //
  11. //replace the characters in the email with five '*' and print         //
  12. //--------------------------------------------------------------------//
  13. //2:                                                                  //
  14. //Phone: If a phone number is detected it will extract every digit    //
  15. //and '+' if detected then based on the amount of digits in the       //
  16. //string determine the number of hyphens neccessary. Once Hyphens     //
  17. //are in place iterate through the string replacing all, but the last //
  18. //four digits with '*'s                                               //
  19. ////////////////////////////////////////////////////////////////////////
  20.  
  21. #include <map>
  22. #include <set>
  23. #include <list>
  24. #include <cmath>
  25. #include <ctime>
  26. #include <deque>
  27. #include <queue>
  28. #include <stack>
  29. #include <string>
  30. #include <bitset>
  31. #include <cstdio>
  32. #include <limits>
  33. #include <vector>
  34. #include <climits>
  35. #include <cstring>
  36. #include <cstdlib>
  37. #include <fstream>
  38. #include <numeric>
  39. #include <sstream>
  40. #include <iostream>
  41. #include <algorithm>
  42. #include <unordered_map>
  43.  
  44. using namespace std;
  45.  
  46. //--------------------------------------------------
  47. //Prototype for masking function
  48. //Used for modular programming
  49. //--------------------------------------------------
  50. void mask(string& input, bool& inputover);
  51.  
  52. int main() {
  53.     //--------------------------------------------------------
  54.     //Driver program used only to manage input
  55.     //--------------------------------------------------------
  56.    
  57.     //Declare and initialize any variables that will be needed
  58.    
  59.     bool inputover = false;  //When boolean is true it will signal
  60.                              //there is no more input to manage.
  61.    
  62.     string input;            //String for input
  63.    
  64.     //Start Loop to get all inputs
  65.     while (!inputover)
  66.     {
  67.         //Get Input and clear Cin to get next input
  68.         getline(cin, input);
  69.         cin.clear();
  70.        
  71.         //===========================================
  72.         //Begin Modular Programming
  73.         //===========================================
  74.        
  75.         //Mask input, and check if program over
  76.         mask(input, inputover);
  77.     }      
  78.     return 0;
  79. }
  80.  
  81. //Function for data masking
  82. void mask(string& input, bool& inputover)
  83. {
  84.     //-----------------------------------------
  85.     //Variable declaration
  86.     //-----------------------------------------
  87.     string email, phone;  //Variables used for manipulating input
  88.     int at;               //Integer will store the position of the  
  89.                           //'@' in an email address from the input
  90.    
  91.     //Use Email Procedure
  92.     if (input[0] == 'E')
  93.     {
  94.         //assign input to email for manipulation
  95.         email = input;
  96.        
  97.         //iterate through email until '@' is found
  98.         for (int i = 3; email[i-1] != '@'; i++)
  99.         {
  100.             //Once '@' is found store it's position
  101.             if (email[i] == '@')
  102.             {
  103.                 at = i;
  104.             }
  105.         }
  106.         //Once position is found simply replace all the characters
  107.         //from the second character to the second to last from the '@'
  108.         // ie. From: abcdefghijklmnopqrstuvwxyz@website.com
  109.         //            ^----------------------^
  110.         //     To:   a*****z@website.com
  111.         //
  112.         email.replace(3, at-4, "*****");
  113.        
  114.         //output result
  115.         cout << email << endl;
  116.     }
  117.     //Use Phone Procedure
  118.     else if (input[0] == 'P')
  119.     {
  120.         //Iterate through input and extract numbers, and +
  121.         //and assign them to phone string for manipulation
  122.         for (int i = 0; i < input.length(); i++)
  123.         {
  124.             if (input[i] > '/' && input[i] < ':' || input[i] == '+')
  125.             {
  126.                 phone += input[i];
  127.             }
  128.         }
  129.         //Insert phone identifier at beginning of phone string
  130.         phone.insert(0, "P:");
  131.        
  132.         //=====================================================
  133.         //applying format, and determining international code
  134.         //=====================================================
  135.         //Note: Standard Phone numbers are length 10 without
  136.         //international code: (XXX) XXX-XXXX
  137.         //if international code is included length will surpass
  138.         //length 10: +XXX (XXX) XXX-XXXX
  139.         //-----------------------------------------------------
  140.        
  141.         //check length for International code and insert hyphen
  142.         //in phone string if International code is there
  143.         if (phone.length() > 12)
  144.         {
  145.             phone.insert(phone.length() - 10, "-");
  146.         }
  147.         //Add standard hyphens that are included on all phone
  148.         //formats regardless of size or international code
  149.         phone.insert(phone.length() - 4, "-");
  150.         phone.insert(phone.length() - 8, "-");
  151.        
  152.         //Mask every number except last 4
  153.         for (int i = 0; i <phone.length() - 4; i++)
  154.         {
  155.             if (isdigit(phone[i]))
  156.             {
  157.                 phone[i] = '*';
  158.             }
  159.         }
  160.        
  161.         //Display results
  162.         cout << phone << endl;
  163.     }
  164.     //If no input or if not email or phone end program
  165.     else
  166.     {
  167.         inputover = true;
  168.     }
  169.    
  170.     //Clear strings for next input
  171.     email.clear();
  172.     phone.clear();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement