Dada una string str , la tarea es cifrar e invertir la string. La string se cifra agregando cada carácter de la string con su índice en la string, es decir, si el carácter ‘a’ está en el índice 2 , entonces el carácter en la string actualizada será ‘a’ + 2 = ‘c’ . Dado que el valor de la string puede ir más allá de 256, realice la suma en el módulo 256.
Ejemplos:
Entrada: str = «geeks»
Salida: wngfg
‘g’ + 0 = ‘g’
‘e’ + 1 = ‘f’
‘e’ + 2 = ‘g’
‘k’ + 3 = ‘n’
‘s’ + 4 = ‘w’
Entrada: str = “java”
Salida: dxbj
Acercarse:
- Agregue cada carácter de la string con su índice dentro de la misma string.
- Invierte la cuerda.
- Imprime la string invertida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the encryptet string string getEncryptedString(string str) { // To build the encrypted string string sb =""; // Traverse the string in reverse for (int i = str.length() - 1; i >= 0; i--) { sb+=(char)((int)str[i] + i) % 256; } // Return the encrypted string return sb; } // Driver code int main() { string str = "geeks"; cout<<getEncryptedString(str)<<endl; return 0; } // This code is contributed by mits
Java
// Java implementation of the approach public class HelloWorld { // Function to return the encryptet string static String getEncryptedString(String str) { // To build the encrypted string StringBuilder sb = new StringBuilder(""); // Traverse the string in reverse for (int i = str.length() - 1; i >= 0; i--) { sb.append("" + (char)((str.charAt(i) + i) % 256)); } // Return the encrypted string return sb.toString(); } // Driver code public static void main(String[] args) { String str = "geeks"; System.out.println(getEncryptedString(str)); } }
Python3
# Python3 implementation of the approach # Function to return the encryptet string def getEncryptedString(str): # To build the encrypted string sb = ""; # Traverse the string in reverse for i in range(len(str) - 1,-1,-1): sb += chr((ord(str[i]) + i) % 256); # Return the encrypted string return sb; # Driver code str = "geeks"; print(getEncryptedString(str)); # This code is contributed by mits
C#
// C# implementation of the approach using System; using System.Text; public class HelloWorld { // Function to return the encryptet string static String getEncryptedString(String str) { // To build the encrypted string StringBuilder sb = new StringBuilder(""); // Traverse the string in reverse for (int i = str.Length - 1; i >= 0; i--) { sb.Append("" + (char)((str[i] + i) % 256)); } // Return the encrypted string return sb.ToString(); } // Driver code public static void Main(String[] args) { String str = "geeks"; Console.WriteLine(getEncryptedString(str)); } } // This code contributed by Rajput-Ji
PHP
<?php // PHP implementation of the approach // Function to return the encryptet string function getEncryptedString($str) { // To build the encrypted string $sb = ""; // Traverse the string in reverse for ($i = strlen($str) - 1; $i >= 0; $i--) { $sb .= chr((ord($str[$i]) + $i) % 256); } // Return the encrypted string return $sb; } // Driver code $str = "geeks"; print(getEncryptedString($str)); // This code is contributed by mits ?>
Javascript
<script> // javascript implementation of the approach // Function to return the encryptet string function getEncryptedString( str) { // To build the encrypted string sb = ""; // Traverse the string in reverse for (i = str.length - 1; i >= 0; i--) { sb+=("" + String.fromCharCode((str.charCodeAt(i) + i) % 256)); } // Return the encrypted string return sb; } // Driver code var str = "geeks"; document.write(getEncryptedString(str)); // This code is contributed by Rajput-Ji </script>
wngfg
Publicación traducida automáticamente
Artículo escrito por Abhishek_Ranjan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA