Given a string containing words forming a sentence(belonging to english language). The task is to output the equivalent ASCII sentence of the input sentence.
ASCII form of a sentence is the conversion of each of the character of the input string and aligning them in position of characters present in the string
Examples:
Input : hello, world!
Output : ASCII Sentence:
104101108108111443211911111410810033
Input : GeeksforGeeks
Output : ASCII Sentence:
7110110110711510211111471101101107115
Explanation:
To complete the task, we need to convert each character into it’s equivalent ASCII value. We perform the following steps to achieve the equivalent ASCII form of the given sentence-
- Iterate over the length of the complete sentence/string
- Take each character of the sentence at a time, subtract NULL character to it and explicitly typecast the result
- Print the result
Following the above steps, we can achieve the equivalent ASCII form of a given sentence/string.
C++
// C++ implementation for converting // a string into it's ASCII equivalent sentence #include <bits/stdc++.h> using namespace std; // Function to compute the ASCII value of each // character one by one void ASCIISentence(std::string str) { int l = str.length(); int convert; for (int i = 0; i < l; i++) { convert = str[i] - NULL; cout << convert; } } // Driver function int main() { string str = "GeeksforGeeks"; cout << "ASCII Sentence:" << std::endl; ASCIISentence(str); return 0; } |
Java
// Java implementation for converting // a string into it's ASCII equivalent sentence import java.util.*; import java.lang.*; class GeeksforGeeks { // Function to compute the ASCII value of each // character one by one static void ASCIISentence(String str) { int l = str.length(); int convert; for (int i = 0; i < l; i++) { convert = str.charAt(i); System.out.print(convert); } } // Driver function public static void main(String args[]) { String str = "GeeksforGeeks"; System.out.println("ASCII Sentence:"); ASCIISentence(str); } } |
Python3
# Python3 implementation for # converting a string into it's # ASCII equivalent sentence # Function to compute the ASCII # value of each character one by one def ASCIISentence( str ): for i in str: print(ord(i), end = '') print('\n', end = '') # Driver code str = "GeeksforGeeks"print("ASCII Sentence:") ASCIISentence(str) # This code iss contributed by "Sharad_Bhardwaj". |
C#
// C# implementation for converting // a string into it's ASCII equivalent sentence using System; class GeeksforGeeks { // Function to compute the ASCII value // of each character one by one static void ASCIISentence(string str) { int l = str.Length; int convert; for (int i = 0; i < l; i++) { convert = str[i]; Console.Write(convert); } } // Driver function public static void Main() { string str = "GeeksforGeeks"; Console.WriteLine("ASCII Sentence:"); ASCIISentence(str); } } // This code is contributed by vt_m. |
PHP
<?php // PHP implementation for converting a // string into it's ASCII equivalent sentence // Function to compute the ASCII // value of each character one by one function ASCIISentence($str) { for ($i = 0; $i< strlen($str); $i++) echo ord($str[$i]); } // Driver code $str = "GeeksforGeeks"; echo "ASCII Sentence:"."\n"; ASCIISentence($str); // This code is contributed // by ChitraNayal ?> |
Output:
ASCII Sentence: 7110110110711510211111471101101107115
The time-complexity of converting into equivalent ASCII sentence is
, where len is the length of the string.
Application:
- Sentence in english language could be encoded/decoded into this form e.g. convert a sentence into it’s equivalent ASCII form and add 5 to each digit and send it from encoder’s side. Later, decoder can subtract 5 from each digit and decode it into it’s original form. This way only the sender and the receiver would be able to decode the sentence.
- ASCII form is also used to transfer data from one computer to another.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert the ASCII value sentence to its equivalent string
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Print each word in a sentence with their corresponding average of ASCII values
- Sums of ASCII values of each word in a sentence
- Frequency of smallest character in first sentence less than that of second sentence
- Split a given string into substrings of length K with equal sum of ASCII values
- Check whether two strings are equivalent or not according to given condition
- Check whether the binary equivalent of a number ends with given string or not
- Convert given Float value to equivalent Fraction
- Split a sentence into words in C++
- Program to print ASCII Value of a character
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Maximize the decimal equivalent by flipping only a contiguous set of 0s
- Print longest palindrome word in a sentence
- Check a given sentence for a given set of simple grammer rules
- Find the word from a given sentence having given word as prefix
- Average of ASCII values of characters of a given string
- Subsequences generated by including characters or ASCII value of characters of given string
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 : chitranayal

