C++ Program to get string typed in different keyboard layout


Suppose we have two string S and T, and another string R. There are two popular keyboard layouts represented in S and T, they differ only in letters positions. All the other keys are the same. In S and T, all keys of the first and the second layouts in the same order. R may have digits also. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. We have to find the string maintaining the second layout. (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 non-alphabetic characters).

Problem Category

To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm

https://www.tutorialspoint.com/cprogramming/c_strings.htm

So, if the input of our problem is like S = "qwertyuiopasdfghjklzxcvbnm"; T = "veamhjsgqocnrbfxdtwkylupzi"; R = "helloworld87nicecoding", then the output will be "xawwqeqmwr87zglalqrgzf"

Steps

To solve this, we will follow these steps −

res := an empty string
for initialize i := 0, when R[i] is not equal to 0, update (increase i by 1), do:
   x := 0
   for initialize j := 0, when j < 26, update (increase j by 1), do:
      if R[i] is same as S[j], then:
         res := res + T[j]
         x := 1
         Come out from the loop
      otherwise when R[i] + 32 is same as S[j], then:
         k := T[j] - 32
         res := res + k
         x := 1
         Come out from the loop
   if x is same as 0, then:
      res := res + R[i]
return res

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
string solve(string S, string T, string R){
   int x;
   string res = "";
   for (int i = 0; R[i] != 0; i++){
      x = 0;
      for (int j = 0; j < 26; j++){
         if (R[i] == S[j]){
            res += T[j];
            x = 1;
            break;
         }
         else if (R[i] + 32 == S[j]){
            char k = T[j] - 32;
            res += k;
            x = 1;
            break;
         }
      }
      if (x == 0)
         res += R[i];
   }
   return res;
}
int main(){
   string S = "qwertyuiopasdfghjklzxcvbnm";
   string T = "veamhjsgqocnrbfxdtwkylupzi";
   string R = "helloworld87nicecoding";
   cout << solve(S, T, R) << endl;
}

Input

"qwertyuiopasdfghjklzxcvbnm", "veamhjsgqocnrbfxdtwkylupzi",
"helloworld87nicecoding"

Output

xawwqeqmwr87zglalqrgzf

Updated on: 08-Apr-2022

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements