给定一个表示ASCII语句的字符串str ,任务是将该字符串转换为其等效的字符序列。
例子:
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!
做法:遍历整个字符串逐个并连接每一个数字。一旦串联的值在[32,122]范围内,我们将在ASCII表中打印与此数字值相对应的字符值。我们采用范围[32,122],因为空格,大写字母,小写字母都在此范围内。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
= 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
输出:
GeeksforGeeks
时间复杂度: O(N),其中N是给定字符串的长度。