The Wayback Machine - https://web.archive.org/web/20241126021647/https://www.geeksforgeeks.org/convert-the-ascii-value-sentence-to-its-equivalent-string/
Open In App

Convert the ASCII value sentence to its equivalent string

Last Updated : 08 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a string str which represents the ASCII (American Standard Code for Information Interchange) 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, and lowercase letters are within this range.

Below is the implementation of the above approach:

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


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>


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


Output

GeeksforGeeks

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.



Previous Article
Next Article

Similar Reads

Print given sentence into its equivalent ASCII form
Given a string containing words forming a sentence (belonging to the English language). The task is to output the equivalent ASCII sentence of the input sentence. ASCII (American Standard Code for Information Interchange) form of a sentence is the conversion of each of the characters of the input string and aligning them in the position of characte
4 min read
Program to convert given Binary to its equivalent ASCII character string
Given a binary string str, the task is to find its equivalent ASCII (American Standard Code for Information Interchange) character string. Examples: Input: str = "0110000101100010"Output: abExplanation: Dividing str into set of 8 bits as follows: 01100001 = 97, ASCII value of 97 is 'a'.01100010 = 98, ASCII value of 98 is 'b'.Therefore, the required
9 min read
Convert a sentence into its equivalent mobile numeric keypad sequence
Given a sentence in the form of a string, convert it into its equivalent mobile numeric keypad sequence.  Examples :  Input: GEEKSFORGEEKSOutput: 4333355777733366677743333557777Explanation: For obtaining a number, we need to press a number corresponding to that character for a number of times equal to the position of the character. For example, for
6 min read
Check if a string contains a non-ASCII value in Julia - ascii() Method
The ascii() is an inbuilt function in Julia which is used to convert a specified string to a String type and also check the presence of ASCII data. If the non-ASCII byte is present, throw an ArgumentError indicating the position of the first non-ASCII byte. Syntax: ascii(s::AbstractString) Parameters:s::AbstractString: Specified stringReturns: It r
1 min read
Convert Hexadecimal value String to ASCII value String
Given the Hexadecimal value string as input, the task is to convert the given hexadecimal value string into its corresponding ASCII format string. Examples:  Input: 6765656b73Output: geeks Input: 6176656e67657273Output: avengers The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system. Being a Base-16 system, there are 16 possi
6 min read
Convert a Mobile Numeric Keypad sequence to equivalent sentence
Given a string S of size N, consisting of digits [0 - 9] and character '.', the task is to print the string that can be obtained by pressing the mobile keypad in the given sequence. Note: '.' represents a break while typing. Below is the image to represent the characters associated with each number in the keypad. Examples: Input: S = "234"Output: A
9 min read
Convert Character value to ASCII value in R Programming - charToRaw() Function
charToRaw() function in R Language is used to convert the given character to their corresponding ASCII value or "raw" objects. Syntax: charToRaw(x)Parameters: x: Given characters to be converted Example 1: C/C++ Code # R program to illustrate # charToRaw function # Calling charToRaw() function over # some alphabets x <- charToRaw("a")
1 min read
Convert the given BCD to its equivalent Binary form
Given a BCD (Binary Coded Decimal), the task is to convert this to its equivalent Binary number. Examples: Input: 1001000 Output: 110000 Explanation: Integer value of the given BCD is 48(0100 -> 4, 1000 -> 8). (48)10 = (110000)2 Input: 1001001 Output: 110000 Approach: In order to solve this problem we need to split the given BCD number into b
6 min read
Program to convert Hexa-Decimal Number to its equivalent BCD
Given a HexaDecimal Number N, the task is to convert the number to its equivalent Binary Coded Decimal.Examples: Input: 11F Output: 0001 0001 1111 Explanation: Binary of 1 - 0001 Binary of F - 1111 Thus, Equivalent BCD is 0001 0001 1111Input: A D Output: 1010 1101 Explanation: Binary of A - 1010 Binary of D - 1101 Thus, Equivalent BCD is 1010 1101
10 min read
What is ASCII - A Complete Guide to Generating ASCII Code
The American Standard Code for Information Interchange, or ASCII, is a character encoding standard that has been a foundational element in computing for decades. It plays a crucial role in representing text and control characters in digital form. Historical BackgroundASCII has a rich history, dating back to its development in the early 1960s. Origi
13 min read
Substring with maximum ASCII sum when some ASCII values are redefined
Given a string W, and two arrays X[] and B[] of size N each where the ASCII value of character X[i] is redefined to B[i]. Find the substring with the maximum sum of the ASCII (American Standard Code for Information Interchange) value of the characters. Note: Uppercase & lowercase both will be present in the string W. Input: W = "abcde", N = 1,
9 min read
ASCII NULL, ASCII 0 ('0') and Numeric literal 0
The ASCII (American Standard Code for Information Interchange) NULL and zero are represented as 0x00 and 0x30 respectively. An ASCII NULL character serves as a sentinel character of strings in C/C++. When the programmer uses '0' in his code, it will be represented as 0x30 in hex form. What will be filled in the binary integer representation in the
3 min read
Print each word in a sentence with their corresponding average of ASCII values
Given a sentence, the task is to find the average of ASCII values of each word in the sentence and print it with the word. Examples: Input: sentence = "Learning a string algorithm"Output:Learning - 102a - 97string - 110algorithm - 107 Approach: Take an empty string and start traversing the sentence letter by letter.Add a letter to the string and ad
6 min read
Sums of ASCII values of each word in a sentence
We are given a sentence of English language(can also contain digits), we need to compute and print the sum of ASCII values of characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASCII each character in a word: 1361 97 879 730 658 327 527 Total sum -
7 min read
Convert given Float value to equivalent Fraction
Given a floating-point number in the form of a string N, the task is to convert the given floating-point number into fractions. Enclosed sequence of digits in "()" in the floating-point representation expresses recurrence in the decimal representation. For example, 1.(6) represents 1.666.... Examples: Input: N = "1.5"Output: 3 / 2Explanation:The va
11 min read
Check if a Float value is equivalent to an Integer value
Given a floating-point number N, the task is to check if the value of N is equivalent to an integer or not. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: N = 1.5Output: NO Input: N = 1.0Output: YES Approach: The idea is to use the concept of Type Casting. Follow the steps below to solve the problem: Initialize a var
3 min read
Frequency of smallest character in first sentence less than that of second sentence
Given two array of strings, arr1[] and arr2[], the task is to count the number of strings in arr2[] whose frequency of the smallest characters is less than the frequency of the smallest character for each string in arr1[]. Examples: Input: arr1[] = {"cbd"}, arr2[] = {"zaaaz"} Output: 1 Explanation: The frequency of the smallest characters in "cbd"
15 min read
Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
Given an integer 'k' and a string 'str' consisting of characters from English alphabets. The task is to convert all lower case character to uppercase whose ASCII value is co-prime with k. Examples: Input: str = "geeksforgeeks", k = 4 Output: GEEKSfOrGEEKS 'f' and 'r' are the only characters whose ASCII values aren't co-prime with 4. Input: str = "A
6 min read
Decimal Equivalent of Gray Code and its Inverse
Given a decimal number n. Find the gray code of this number in decimal form. Examples: Input : 7 Output : 4 Explanation: 7 is represented as 111 in binary form. The equivalent gray code of 111 is 100 in binary form, whose decimal equivalent is 4. Input : 10 Output : 15 Explanation: 10 is represented as 1010 in binary form. The equivalent gray code
7 min read
Program to convert polar co-ordinates to equivalent cartesian co-ordinates
Given two integers r and [Tex]\theta[/Tex](in degree) representing polar coordinates of a point (r, [Tex]\theta[/Tex]), the task is to find the Cartesian coordinates of the given point. Examples: Input: r = 1.4142, [Tex]\theta[/Tex] = 45Output: 1.000, 1.000 Input: r = 3, [Tex]\theta[/Tex] = 30Output: 2.598, 1.500 Approach: Let the cartesian coordin
4 min read
Increase the ASCII value of characters of the string by the given values
Content Removed
1 min read
Subsequences generated by including characters or ASCII value of characters of given string
Given a string str of length N, the task is to print all possible non-empty subsequences of the given string such that the subsequences either contain characters or ASCII values of the characters from the given string. Examples: Input: str = "ab" Output: b 98 a ab a98 97 97b 9798 Explanation: Possible subsequence of the strings are { b, a, ab }. Po
6 min read
Count pairs of characters in a string whose ASCII value difference is K
Given string str of lower case alphabets and a non-negative integer K. The task is to find the number of pairs of characters in the given string whose ASCII value difference is exactly K. Examples: Input: str = "abcdab", K = 0 Output: 2 (a, a) and (b, b) are the only valid pairs.Input: str = "geeksforgeeks", K = 1 Output: 8 (e, f), (e, f), (f, e),
7 min read
Replace every character of string by character whose ASCII value is K times more than it
Given string str consisting of lowercase letters only and an integer k, the task is to replace every character of the given string with a character whose ASCII value is k times more than it. If the ASCII value exceeds 'z', then start checking from 'a in a cyclic manner. Examples: Input: str = "abc", k = 2 Output: cde Explanation: a is moved by 2 ti
9 min read
Decode an Encoded Base 64 String to ASCII String
Prerequisite : What is base64 Encoding and why we encode strings to base64 formatBase64 encoding is performed at sending node before transmitting bits over a network, and receiving node decodes that encoded data back to original ASCII string. Base64 character set is // 64 characterschar_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012
15+ min read
Check whether the binary equivalent of a number ends with given string or not
Given a positive integer N, the task is to check whether the binary equivalent of that integer ends with the given string str or not. Print "Yes" if it ends in "str". Otherwise, Print "No".Examples: Input: N = 23, str = "111"Output: YesExplanation:Binary of 23 = 10111, which ends with "111" Input: N = 5, str = "111"Output: No Approach: The idea is
5 min read
Node having maximum number of nodes less than its value in its subtree
Given a Binary Tree, the task is to find the node from the given tree which has the maximum number of nodes in its subtree with values less than the value of that node. In the case of multiple possible nodes with the same number of maximum nodes, then return any such node. Examples: Input: 4 / \ 6 10 / \ / \2 3 7 14 / 5 Output: 6Explanation:Node wi
10 min read
Lexicographically largest array possible from first N natural numbers such that every repetition is present at distance equal to its value from its previous occurrence
Given a positive integer N, the task is to construct the lexicographically largest array of size (2 * N - 1) comprising of first N natural numbers such that each element occurs twice except 1 and the repetition of X is exactly X distance apart in the constructed array. Examples: Input: N = 4Output: 4 2 3 2 4 3 1Explanation:For the generated array {
10 min read
Find GCD of all Array numbers for which its value is equal to its frequency
Given an array arr[] of integers N, the task is to find the GCD of all numbers for which it's value is equal to its frequency in the array. Examples; Input: arr={2, 3, 4, 2, 3, 3} N=6 Output: 1Explanation: Here 2 is occurring 2 times and 3 is occurring 3 times. Hence GCD of 2, 3, is 1. So our output is 1. Input: arr={2, 3, 4, 4, 3, 5, 4, 4, 2, 8} N
6 min read
Program to Convert ASCII to Unicode
In this article, we will learn about different character encoding techniques which are ASCII (American Standard Code for Information Interchange) and Unicode (Universal Coded Character Set), and the conversion of ASCII to Unicode. Table of Content What is ASCII Characters?What is ASCII Table?What is Unicode?Need for ASCII to Unicode ConversionAlgor
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg