Given a string, task is to write a function translate() that will translate a text into “rovarspraket” (Swedish for “robber’s language”). That is, double every consonant and place an occurrence of “o” in between.
Examples:
Input : this is fun
Output : tothohisos isos fofunon
t is consonant then double the consonant and place “o” in between,
So it becomes “tot” and do this for full stringInput : neveropen
Output : gogeekoksos
Implementation:
C++
// C++ implementation to Encrypt a // string into the rovarspraket (Robber Language) #include <iostream> using namespace std; // Function return translated string string translate(string a) { // Length of the string int len = a.length(); string res = "" ; // Run till length of string for ( int i = 0; i < len; i++) { // checking if character is vowel, // if yes then append it as it is if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' ) { res = res + a[i]; } // if space then append as it is else if (a[i] == ' ' ) { res = res + a[i]; } // else double the consonant and put o in between else { res = res + a[i] + 'o' + a[i]; } } // return translated string return res; } // Driver Code int main() { string str = "neveropen for neveropen" ; // Calling function cout << translate(str) << endl; return 0; } |
Java
// Java implementation to Encrypt a // String into the rovarspraket (Robber Language) import java.util.*; class GFG { // Function return translated String static String translate(String a) { // Length of the String int len = a.length(); String res = "" ; // Run till length of String for ( int i = 0 ; i < len; i++) { // checking if character is vowel, // if yes then append it as it is if (a.charAt(i) == 'a' || a.charAt(i) == 'e' || a.charAt(i) == 'i' || a.charAt(i) == 'o' || a.charAt(i) == 'u' ) { res = res + a.charAt(i); } // if space then append as it is else if (a.charAt(i) == ' ' ) { res = res + a.charAt(i); } // else double the consonant and // put o in between else { res = res + a.charAt(i) + 'o' + a.charAt(i); } } // return translated String return res; } // Driver Code public static void main(String[] args) { String str = "neveropen for neveropen" ; // Calling function System.out.println(translate(str)); } } // This code is contributed by PrinciRaj1992 |
Python
# Python implementation to Encrypt a # string into the rovarspraket (Robber Language) def translate(a): c = 0 x = "" # Count length of string for i in a: c + = 1 for i in range ( 0 , c): # If alphabet is vowel, do not change if a[i] = = 'a' or a[i] = = 'e' or a[i] = = 'i' or a[i] = = 'o' or a[i] = = 'u' : b = a[i] x + = b # else double the consonant and put 'O' in between the alphabet else if a[i] ! = " " : b = a[i] + 'o' + a[i] x + = b # if string has space than put space else if a[i] = = " " : x + = a[i] # print string print (x) s = "neveropen for neveropen" translate(s) |
C#
// C# implementation to Encrypt a // String into the rovarspraket (Robber Language) using System; class GFG { // Function return translated String static String translate(String a) { // Length of the String int len = a.Length; String res = "" ; // Run till length of String for ( int i = 0; i < len; i++) { // checking if character is vowel, // if yes then append it as it is if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' ) { res = res + a[i]; } // if space then append as it is else if (a[i] == ' ' ) { res = res + a[i]; } // else double the consonant and put o in // between else { res = res + a[i] + 'o' + a[i]; } } // return translated string return res; } // Driver Code public static void Main(String[] args) { String str = "neveropen for neveropen" ; // Calling function Console.WriteLine(translate(str)); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // JavaScript implementation to Encrypt a // string into the rovarspraket (Robber Language) // Function return translated string function translate(a) { // Length of the string let len = a.length; let res= "" ; // Run till length of string for (let i=0; i<len ;i++) { // checking if character is vowel, // if yes then append it as it is if (a[i] == 'a' || a[i]== 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' ) { res = res + a[i]; } // if space then append as it is else if (a[i] == ' ' ) { res = res +a[i]; } // else double the consonant and put o in between else { res =res+ a[i] + 'o' + a[i]; } } // return translated string return res; } // Driver Code let str = "neveropen for neveropen" ; // Calling function document.write(translate(str), "</br>" ); // This code is contributed by shinjanpatra </script> |
gogeekoksos foforor gogeekoksos
Time Complexity: O(len), //In the above-given approach, there is one loop for iterating over string which takes O(Len) time in worst case. Therefore, the time complexity for this approach will be O(Len).
Auxiliary Space: O(len) // since we are using an empty string for storing the characters hence the space taken is linear
This article is contributed by Sahil Rajput. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!