Related Articles
Change string to a new character set
• Difficulty Level : Medium
• Last Updated : 10 Jan, 2020

Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd….xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set.

Examples:

```New character set : qwertyuiopasdfghjklzxcvbnm
Input : "utta"
Output : geek

Input : "egrt"
Output : code
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Idea behind conversion of new character set is to use hashing. Perform hashing of new character set where element of set is index and its position will be new alphabet value.

Approach1:

Given New character set = “qwertyuiopasdfghjklzxcvbnm”

1. First character is q, during hashing we will place ‘a’ (for position ) at index q i.e. (17th).
2. After hashing our new character set is “kvmcnophqrszyijadlegwbuft”.
3. For input “egrt” =
hash[e -‘a’] = c
hash[g -‘a’] = o
hash[r -‘a’] = d
hash[t -‘a’] = e

For “egrt” is equivalent to “code”.

## C++

 `// CPP program to change the sentence``// with virtual dictionary ``#include``using` `namespace` `std;`` ` `// Converts str to given character set``void` `conversion(``char` `charSet[], string &str)``{ ``    ``int` `n = str.length();`` ` `    ``// hashing for new character set``    ``char` `hashChar;``    ``for` `(``int` `i = 0; i < 27; i++)    ``        ``hashChar[charSet[i]-``'a'``] = ``'a'` `+ i;    ``     ` `    ``// conversion of new character set``    ``for` `(``int` `i = 0; i < n; i++)``        ``str[i] = hashChar[str[i]-``'a'``];``}`` ` `// Driver code``int` `main()``{``    ``char` `charSet = ``"qwertyuiopasdfghjklzxcvbnm"``;``    ``string str = ``"egrt"``; ``    ``conversion(charSet, str);``    ``cout << str;``    ``return` `0;``}`

## Java

 `// Java program to change the sentence``// with virtual dictionary ``class` `GFG {`` ` `// Converts str to given character set``    ``static` `String conversion(``char` `charSet[], String str) {``        ``int` `n = str.length();`` ` `        ``// hashing for new character set``        ``char` `hashChar[] = ``new` `char``[``26``];``        ``for` `(``int` `i = ``0``; i < ``26``; i++) {``             ` `            ``hashChar[Math.abs(charSet[i] - ``'a'``)] = (``char``) (``'a'` `+ i);``        ``}`` ` `        ``// conversion of new character set``        ``String s=``""``;``        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``s += hashChar[str.charAt(i) - ``'a'``];``        ``}``        ``return` `s;``    ``}`` ` `// Driver code``    ``public` `static` `void` `main(String[] args) {``        ``char` `charSet[] = ``"qwertyuiopasdfghjklzxcvbnm"``.toCharArray();``        ``String str = ``"egrt"``;``        ``str = conversion(charSet, str);``        ``System.out.println(str);``// This code is contributed by princiRaj1992``    ``}``}`

## C#

 `// C# program to change the sentence``// with virtual dictionary ``using` `System;``     ` `class` `GFG ``{`` ` `    ``// Converts str to given character set``    ``static` `String conversion(``char` `[]charSet, ``                             ``String str) ``    ``{``        ``int` `n = str.Length;`` ` `        ``// hashing for new character set``        ``char` `[]hashChar = ``new` `char``;``        ``for` `(``int` `i = 0; i < 26; i++) ``        ``{``            ``hashChar[Math.Abs(charSet[i] - ``'a'``)] = (``char``) (``'a'` `+ i);``        ``}`` ` `        ``// conversion of new character set``        ``String s = ``""``;``        ``for` `(``int` `i = 0; i < n; i++) ``        ``{``            ``s += hashChar[str[i] - ``'a'``];``        ``}``        ``return` `s;``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args) ``    ``{``        ``char` `[]charSet = ``"qwertyuiopasdfghjklzxcvbnm"``.ToCharArray();``        ``String str = ``"egrt"``;``        ``str = conversion(charSet, str);``        ``Console.WriteLine(str);``    ``}``}`` ` `// This code is contributed by Princi Singh`

Output:
```code
```

Approach2:
1.Initialize two strings, one with actual set of alphabets and another with modified one.
2.Get the string to be converted from the user.
3.Retrive the first element of the string, find its index in the modified set of alphabets(eg:0 for ‘q’).
4.Find the element of same index in the actual set of alphabets and concatenate it with the result string.
5.Repeat the above steps for all the remaining elements of the input string.
6.Return the result string.

## Java

 `// Java program to change the sentence``// with virtual dictionary `` ` `class` `GFG ``{``    ``static` `char``[] alphabets = ``"abcdefghijklmnopqrstuvwxyz"``.toCharArray();`` ` `    ``// function for converting the string``    ``static` `String conversion(String charSet, ``char``[] str1)``    ``{``        ``String s2 = ``""``;``        ``for` `(``char` `i : str1)``         ` `            ``// find the index of each element of the``            ``// string in the modified set of alphabets``            ``// replace the element with the one having the``            ``// same index in the actual set of alphabets``            ``s2 += alphabets[charSet.indexOf(i)];`` ` `        ``return` `s2;``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String charSet = ``"qwertyuiopasdfghjklzxcvbnm"``;``        ``String str1 = ``"egrt"``;``        ``System.out.print(conversion(charSet, str1.toCharArray()));``    ``}``}`` ` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 program to change the sentence``#  with virtual dictionary `` ` `#function for converting the string``def` `conversion(charSet,str1):``    ``s2``=``""``    ``for` `i ``in` `str1:``        ``# find the index of each element of the``        ``# string in the modified set of alphabets``        ``# replace the element with the one having the``        ``# same index in the actual set of alphabets``        ``s2 ``+``=` `alphabets[charSet.index(i)]``         ` `    ``return` `s2`` ` `# Driver Code``if` `__name__``=``=``'__main__'``:``    ``alphabets ``=` `"abcdefghijklmnopqrstuvwxyz"``    ``charSet``=` `"qwertyuiopasdfghjklzxcvbnm"``    ``str1 ``=` `"egrt"``    ``print``(conversion(charSet,str1))`` ` `#This code is contributed by PradeepEswar`

## C#

 `// C# program to change the sentence``// with virtual dictionary ``using` `System;`` ` `class` `GFG ``{``    ``static` `char``[] alphabets = ``"abcdefghijklmnopqrstuvwxyz"``.ToCharArray();`` ` `    ``// function for converting the string``    ``static` `String conversion(String charSet, ``char``[] str1)``    ``{``        ``String s2 = ``""``;``        ``foreach` `(``char` `i ``in` `str1)``         ` `            ``// find the index of each element of the``            ``// string in the modified set of alphabets``            ``// replace the element with the one having the``            ``// same index in the actual set of alphabets``            ``s2 += alphabets[charSet.IndexOf(i)];`` ` `        ``return` `s2;``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``String charSet = ``"qwertyuiopasdfghjklzxcvbnm"``;``        ``String str1 = ``"egrt"``;``        ``Console.Write(conversion(charSet, str1.ToCharArray()));``    ``}``}`` ` `// This code is contributed by Rajput-Ji`

Output:
```code
```
My Personal Notes arrow_drop_up