/* This program converts a 7 letter word, (all uppercase) into a telephone number. For example, "PROGRAM" becomes 776-4726. Two arrays are used: I, which is an array of 7 characters, holds the user input (the 7 letter word). The array D is an array of 26 integers. The INDEX or position of this array is determined by the letter in the alphabet - A will be position 0, B position 1, Z position 25, etc... We get the position from the charater by calling the chord function, which is defined before main. This function converts the character first into its ascii value, then subtract 65 from it to get it within the range 0-25 (65 is the ascii value of 'A'). It also returns -1 if the charater is not upper case. The VALUE or contents of the array D will be the digits that correspond to each character. For example D[0] should contain 2, since 'A' is associated with the digit 2. D[25] should contain 9, since 'Z' is associated with digit 9. THE INDEX IS THE LETTER, THE CONTENT IS THE DIGIT. In main, The first for loop, plus the 3 additional lines beneath, places the appropriate information into the array D, so that D[0], D[1], D[2] (corresponding to A, B, C), will have value 2, D[3], D[4], D[5] (corresponding to D, E, F) will have value 3, etc... After this array is constructed, we read in the 7 letter word letter by letter into the array I, then use a loop to translate each letter into a digit. This is done by looking up the array D for each letter, after calling the chord function to convert the letter to an array position. */ #include // convert a char to an array index A -> 0 B -> 1, C -> 2, etc... int chord(char c1) { int asciic1; asciic1 = (int) c1; // if c1 is 'A' then asciic1 will be 65 if ( asciic1 >=65 && asciic1 <=90) return asciic1 - 65; else return -1; // -1 indicates invalid char } // x = 2; y =2; x = y = 2; int main() { int i; // loop counter char I[7]; // input holds 7 char word int digit; int D[26]; // characters as indices, digits as values // instantiate array D with appropriate info: digit = 2; for(i=0;i<18;i=i+3) // from A to R { D[i] = digit; D[i+1] = digit; D[i+2] = digit; digit++; } // i== 18, digit == 8 at this point D[18] = 7; // 18 is S D[19] = D[20] = D[21] = 8; // tuv D[22] = D[23] =D[24] = D[25] = 9; // wxyz // finished constructing array D cout << "enter a 7 character word: "; cin >> I[0] >> I[1]>> I[2]>> I[3]>> I[4]>> I[5]>> I[6]; // I will contain somthing like "PROGRAM" cout << "the number is "; i = 0; while (i<7) { if (i== 3) cout << "-"; if (chord(I[i])>=0) // upper case char? cout << D[ chord( I[i] ) ]; // translate else cout << I[i]; // leave non-upper case letters alone i++; } cout << endl; return 0; }