Encrypt a string into the Rovarspraket (The Robber Language)
Last Updated :
24 Apr, 2023
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 string
Input : geeks
Output : gogeekoksos
Implementation:
C++
#include <iostream>
using namespace std;
string translate(string a)
{
int len = a.length();
string res = "" ;
for ( int i = 0; i < len; i++) {
if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i'
|| a[i] == 'o' || a[i] == 'u' ) {
res = res + a[i];
}
else if (a[i] == ' ' ) {
res = res + a[i];
}
else {
res = res + a[i] + 'o' + a[i];
}
}
return res;
}
int main()
{
string str = "geeks for geeks" ;
cout << translate(str) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static String translate(String a)
{
int len = a.length();
String res = "" ;
for ( int i = 0 ; i < len; i++) {
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);
}
else if (a.charAt(i) == ' ' ) {
res = res + a.charAt(i);
}
else {
res = res + a.charAt(i) + 'o' + a.charAt(i);
}
}
return res;
}
public static void main(String[] args)
{
String str = "geeks for geeks" ;
System.out.println(translate(str));
}
}
|
Python
def translate(a):
c = 0
x = ""
for i in a:
c + = 1
for i in range ( 0 , c):
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 if a[i] ! = " " :
b = a[i] + 'o' + a[i]
x + = b
else if a[i] = = " " :
x + = a[i]
print (x)
s = "geeks for geeks"
translate(s)
|
C#
using System;
class GFG {
static String translate(String a)
{
int len = a.Length;
String res = "" ;
for ( int i = 0; i < len; i++) {
if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i'
|| a[i] == 'o' || a[i] == 'u' ) {
res = res + a[i];
}
else if (a[i] == ' ' ) {
res = res + a[i];
}
else {
res = res + a[i] + 'o' + a[i];
}
}
return res;
}
public static void Main(String[] args)
{
String str = "geeks for geeks" ;
Console.WriteLine(translate(str));
}
}
|
Javascript
<script>
function translate(a)
{
let len = a.length;
let res= "" ;
for (let i=0; i<len ;i++)
{
if (a[i] == 'a' || a[i]== 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' )
{
res = res + a[i];
}
else if (a[i] == ' ' )
{
res = res +a[i];
}
else
{
res =res+ a[i] + 'o' + a[i];
}
}
return res;
}
let str = "geeks for geeks" ;
document.write(translate(str), "</br>" );
</script>
|
Output
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
Share your thoughts in the comments
Please Login to comment...