Friday, July 15, 2016

Cleanse Phone Numbers (C++)

Recently I came across a question. The question is as follows.
1. There are several phone numbers (only US phone numbers). All the US phone numbers are basically in the format xxx xxx xxxx which is area code and 7 digit phone number. The number can have spaces between area code, first three digits of phone number or may be without spaces
2. The catch is that each phone number is re-arranged in a way that first three digits of phone number comes first then area code and then the last four digits.

Example:
Phone number is 425 123 4567 or 4251234567. This is the correct representation. But its stored as 123 425 4567 or 1234254567. This needs to be fixed and represented as the correct format. C++ code to cleanse the data. This is the quick solution that I got to. There may be several ways to achieve this


// FirstProject.cpp : Defines the entry point for the console application.
// This program assumes that the phone numbers are entered in the wrong order instead of area code and phone number its xxxareacodeandlastfournumbers
//Some times people may enter space/spaces between area code and first three numbers of phone.
//This program gets all the phone numbers into a vector, removes any spaces between numbers in the string and re-arranges values in the right order

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>


using namespace std;

//function prototype
void cleansePhoneNumbers(vector<string>&inputVector);


int main()
{
       vector<string> phoneNumbers;
       string phone = "";

       while (true) //start of infinite loop to add data to vector
       {
              cout << "Enter a phone number (press enter to exit):";
              getline(cin, phone); //get the data into phone string including spaces
              if (phone == "") //if just Enter key is pressed at the prompt
              {
                     break; //loop breaks
              }
              phoneNumbers.push_back(phone); //append the current value in phone to the vector
       }
      
       cleansePhoneNumbers(phoneNumbers); //function call to cleanse phone numbers


       for (string st : phoneNumbers) //for each string at the index of the vector
       {
              cout << "Cleansed Phone Numbers: " << st << endl; //print the value at that index
       }
       return 0;
}

//function definition
void cleansePhoneNumbers(vector<string>&inputVector)
{
       string *temp; //pointer to store address of each index of the vector
       int size;
       string val;
       size = inputVector.size(); //get the size of vector and assign it to size. Length of the vector
      
       for (int i = 0; i < size; i++) //traverse through each index
       {
              temp = &inputVector[i]; //get the address of index into temp pointer. This will be used to update the value in the vector directly
              val = inputVector[i]; //get the value at that index
      
              while (val.find(" ") != val.npos) //loop to remove any spaces in the string at a given idex
              {
                     val.replace(val.find(" "), 1, "");
              }

              val = val.substr(3, 3) + val.substr(0, 3) + val.substr(6, 4); //change the value
             
              *temp = val; //update value in the temp pointer so that actual data in the vector at that index changes

       }

}