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++
#include <bits/stdc++.h>
using namespace std;
void asciiToSentence(string str, int len)
{
int num = 0;
for ( int i = 0; i < len; i++) {
num = num * 10 + (str[i] - '0' );
if (num >= 32 && num <= 122) {
char ch = ( char )num;
cout << ch;
num = 0;
}
}
}
int main()
{
string str = "7110110110711510211111471101101107115" ;
int len = str.length();
asciiToSentence(str, len);
return 0;
}
|
Java
class GFG {
static void asciiToSentence(String str, int len)
{
int num = 0 ;
for ( int i = 0 ; i < len; i++) {
num = num * 10 + (str.charAt(i) - '0' );
if (num >= 32 && num <= 122 ) {
char ch = ( char )num;
System.out.print(ch);
num = 0 ;
}
}
}
public static void main(String args[])
{
String str = "7110110110711510211111471101101107115" ;
int len = str.length();
asciiToSentence(str, len);
}
}
|
Python3
def asciiToSentence(string, length) :
num = 0 ;
for i in range (length) :
num = num * 10 + ( ord (string[i]) -
ord ( '0' ));
if (num > = 32 and num < = 122 ) :
ch = chr (num);
print (ch, end = "");
num = 0 ;
if __name__ = = "__main__" :
string = "7110110110711510211111471101101107115" ;
length = len (string);
asciiToSentence(string, length);
|
C#
using System;
class GFG {
static void asciiToSentence(String str, int len)
{
int num = 0;
for ( int i = 0; i < len; i++) {
num = num * 10 + (str[i] - '0' );
if (num >= 32 && num <= 122) {
char ch = ( char )num;
Console.Write(ch);
num = 0;
}
}
}
public static void Main()
{
String str = "7110110110711510211111471101101107115" ;
int len = str.Length;
asciiToSentence(str, len);
}
}
|
PHP
<?php
function asciiToSentence( $string , $length )
{
$num = 0;
for ( $i = 0; $i < $length ; $i ++)
{
$num = $num * 10 + (ord( $string [ $i ]) - ord( '0' ));
if ( $num >= 32 && $num <= 122)
{
$ch = chr ( $num );
print ( $ch );
$num = 0;
}
}
}
$string = "7110110110711510211111471101101107115" ;
$length = strlen ( $string );
asciiToSentence( $string , $length );
?>
|
Javascript
<script>
function asciiToSentence(str, len)
{
var num = 0;
for ( var i = 0; i < len; i++) {
num = num * 10 + (str[i] - '0' );
if (num >= 32 && num <= 122) {
var ch = String.fromCharCode(num);
document.write(ch);
num = 0;
}
}
}
var str = "7110110110711510211111471101101107115" ;
var len = str.length;
asciiToSentence(str, len);
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time, where N is the length of the given string.
Auxiliary Space: O(1), as we are not using any extra space.