int main(int argc, char** argv) { FILE* fp; if(argc != 2) { printf("USAGE: TelephoneWords <fileContainingTestVectors>\n"); return 1; } fp = fopen(argv[1], "r"); if(fp == NULL) { printf("Failed to open the input file '%s' for reading!\n", argv[1]); return 2; } while(!feof(fp)) { numLetters = 0; firstWord = true; do { if(feof(fp)) { break; } fscanf(fp, "%c", &(digits[numLetters])); ++numLetters; } while(digits[numLetters-1] != '\n'); --numLetters; if(numLetters <= 0) { break; } printTelephoneWords(); printf("\n"); } fclose(fp); return 0; }
int main(){ // O(3^n) complexity int phoneNum[] = {4, 9, 7, 1, 9, 2, 7}; printTelephoneWords(phoneNum); printTelephoneWords2(phoneNum); }
void printTelephoneWords(int start=0) { if(start == numLetters) { if(!firstWord) { printf(","); } for(int i=0;i<numLetters;++i) { printf("%c", str[i]); } firstWord = false; return; } int d = char2digit(digits[start]); char* letters = words[d]; while(*letters != '\0') { str[start] = *letters; printTelephoneWords(start+1); ++letters; } }