|
| 1 | +There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. |
| 2 | +In Berland they use alphabet with 26 letters which coincides with English alphabet. |
| 3 | + |
| 4 | +You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. |
| 5 | + |
| 6 | +You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, |
| 7 | +but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout. |
| 8 | + |
| 9 | +Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters. |
| 10 | + |
| 11 | +Input |
| 12 | +The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout. |
| 13 | + |
| 14 | +The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout. |
| 15 | + |
| 16 | +The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. |
| 17 | +The length of s does not exceed 1000. |
| 18 | + |
| 19 | +Output |
| 20 | +Print the text if the same keys were pressed in the second layout. |
| 21 | + |
| 22 | +----------------------------------------------------------- |
| 23 | + |
| 24 | +I did this by making a <char, int> map ... the first keyboard's characters are mapped to a position number. |
| 25 | +Then, when the input is read ... I display second_keyboard[map[i]] ...But, it can be done easier and better with a <char, char> map. |
| 26 | + |
| 27 | +That way ... program is easier and cleaner. |
| 28 | + |
| 29 | +If the character is not an alphabet, just display it as it is. Otherwise, check if it is capital or not and print the corresponding character after checking case. |
| 30 | + |
| 31 | +-------------------------------------------------- |
| 32 | + |
| 33 | +int main() |
| 34 | +{ |
| 35 | + char keyboard_1[NO_OF_ALPHABETS], keyboard_2[NO_OF_ALPHABETS]; |
| 36 | + scanf("%s %s", keyboard_1, keyboard_2); |
| 37 | + |
| 38 | + map <char, char> corresponding_keyboard_2_char; |
| 39 | + for(int i = 0; i < NO_OF_ALPHABETS; i++) |
| 40 | + { |
| 41 | + corresponding_keyboard_2_char[ keyboard_1[i] ] = keyboard_2[i]; |
| 42 | + } |
| 43 | + |
| 44 | + char text[MAX_LENGTH]; |
| 45 | + scanf("%s", text); |
| 46 | + |
| 47 | + for(int i = 0; text[i] != '\0'; i++) |
| 48 | + { |
| 49 | + if(isalpha(text[i])) |
| 50 | + { |
| 51 | + char input = tolower(text[i]); |
| 52 | + putchar(is_capital(text[i]) ? tocapital(corresponding_keyboard_2_char[input]) : corresponding_keyboard_2_char[input]); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + putchar(text[i]); //Not there in the keyboards |
| 57 | + } |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
0 commit comments