Sunday, October 14, 2012

Decode a phone number

Phones have keyboard with letters, decode a phone number

const char* code[]={{"0"}, {"1"}, {"abc"}, {"def"}, {"ghi"}, {"jkl"}, {"mno"}, {"pqrs"}, {"tuv"}, {"wxyz"}};  

void decode(std::string & inputString, std::string result, int i, int n)
{
 std::string value;

 if (n == 0){
 
  std::cout << std::endl;
  for(std::string::const_iterator it = result.begin(); it != result.end(); ++it) { std::cout << *it ;}
  std::cout << std::endl;
  
 } else {

  char c = inputString.at(i);
  int v = atoi(&c);
 
  for (const char *p = code[v]; *p != '\0'; ++p)
   decode (inputString, result + *p, i+1 , n-1);
 }
}

No comments:

Post a Comment