Convert the ASCII value sentence to its equivalent string

Given a string str which represents the ASCII Sentence, the task is to convert this string into its equivalent character sequence.

Examples:

Input: str = “71101101107115”
Output: Geeks
71, 101, 101, 107 are 115 are the unicode values
of the characters ‘G’, ‘e’, ‘e’, ‘k’ and ‘s’ respectively.

Input: str = “104101108108111443211911111410810033”
Output: hello, world!

Approach: Traverse the complete string character by character and concatenate every digit. Once the value of the concatenation comes in the range [32, 122] we print the character value corresponding to this numeric value in the ASCII table. We are taking the range [32, 122] because spaces, uppercase letters, lowercase letters are within this range.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

GeeksforGeeks

Time Complexity: O(N) where N is the length of the given string.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Mithun Kumar