Dada una string str que representa la oración ASCII , la tarea es convertir esta string en su secuencia de caracteres equivalente.
Ejemplos:
Entrada: str = “71101101107115”
Salida: Geeks
71, 101, 101, 107 son 115 son los valores Unicode
de los caracteres ‘G’, ‘e’, ’e’, ’k’ y ‘s’ respectivamente.
Entrada: str = “104101108108111443211911111410810033”
Salida: ¡hola, mundo!
Enfoque: recorrer la string completa carácter por carácter y concatenar cada dígito. Una vez que el valor de la concatenación entra en el rango [32, 122] imprimimos el valor del carácter correspondiente a este valor numérico en la tabla ASCII. Estamos tomando el rango [32, 122] porque los espacios, las letras mayúsculas y las letras minúsculas están dentro de este rango.
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 print the character sequence // for the given ASCII sentence void asciiToSentence(string str, int len) { int num = 0; for (int i = 0; i < len; i++) { // Append the current digit num = num * 10 + (str[i] - '0'); // If num is within the required range if (num >= 32 && num <= 122) { // Convert num to char char ch = (char)num; cout << ch; // Reset num to 0 num = 0; } } } // Driver code int main() { string str = "7110110110711510211111471101101107115"; int len = str.length(); asciiToSentence(str, len); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the character sequence // for the given ASCII sentence static void asciiToSentence(String str, int len) { int num = 0; for (int i = 0; i < len; i++) { // Append the current digit num = num * 10 + (str.charAt(i) - '0'); // If num is within the required range if (num >= 32 && num <= 122) { // Convert num to char char ch = (char)num; System.out.print(ch); // Reset num to 0 num = 0; } } } // Driver code public static void main(String args[]) { String str = "7110110110711510211111471101101107115"; int len = str.length(); asciiToSentence(str, len); } }
Python3
# Python3 implementation of the approach # Function to print the character sequence # for the given ASCII sentence def asciiToSentence(string, length) : num = 0; for i in range(length) : # Append the current digit num = num * 10 + (ord(string[i]) - ord('0')); # If num is within the required range if (num >= 32 and num <= 122) : # Convert num to char ch = chr(num); print(ch, end = ""); # Reset num to 0 num = 0; # Driver code if __name__ == "__main__" : string = "7110110110711510211111471101101107115"; length = len(string); asciiToSentence(string, length); # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; class GFG { // Function to print the character sequence // for the given ASCII sentence static void asciiToSentence(String str, int len) { int num = 0; for (int i = 0; i < len; i++) { // Append the current digit num = num * 10 + (str[i] - '0'); // If num is within the required range if (num >= 32 && num <= 122) { // Convert num to char char ch = (char)num; Console.Write(ch); // Reset num to 0 num = 0; } } } // Driver code public static void Main() { String str = "7110110110711510211111471101101107115"; int len = str.Length; asciiToSentence(str, len); } }
PHP
<?php // PHP implementation of the approach // Function to print the character sequence // for the given ASCII sentence function asciiToSentence($string, $length) { $num = 0; for($i = 0; $i < $length; $i++) { // Append the current digit $num = $num * 10 + (ord($string[$i]) - ord('0')); // If num is within the required range if ($num >= 32 && $num <= 122) { // Convert num to char $ch = chr($num); print($ch); // Reset num to 0 $num = 0; } } } // Driver code $string = "7110110110711510211111471101101107115"; $length = strlen($string); asciiToSentence($string, $length); // This code is contributed by mits ?>
Javascript
<script> // Javascript implementation of the approach // Function to print the character sequence // for the given ASCII sentence function asciiToSentence(str, len) { var num = 0; for (var i = 0; i < len; i++) { // Append the current digit num = num * 10 + (str[i] - '0'); // If num is within the required range if (num >= 32 && num <= 122) { // Convert num to char var ch = String.fromCharCode(num); document.write(ch); // Reset num to 0 num = 0; } } } // Driver code var str = "7110110110711510211111471101101107115"; var len = str.length; asciiToSentence(str, len); </script>
GeeksforGeeks
Complejidad de tiempo: O (N), ya que estamos usando un bucle para atravesar N veces, por lo que nos costará O (N) tiempo, donde N es la longitud de la string dada.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.