📜  将给定的句子打印成其等效的 ASCII 形式

📅  最后修改于: 2022-05-13 01:57:08.675000             🧑  作者: Mango

将给定的句子打印成其等效的 ASCII 形式

给定一个包含组成句子的单词的字符串(属于英语)。任务是输出输入句子的等效 ASCII 句子。
句子的 ASCII 形式是输入字符串的每个字符的转换,并将它们与字符串中存在的字符的位置对齐
例子:

Input : hello, world!
Output : ASCII Sentence:
         104101108108111443211911111410810033

Input : GeeksforGeeks
Output : ASCII Sentence:
         7110110110711510211111471101101107115

解释:
为了完成这个任务,我们需要将每个字符转换成它对应的 ASCII 值。我们执行以下步骤来实现给定句子的等效 ASCII 形式-

  • 遍历整个句子/字符串的长度
  • 一次取句子的每个字符,减去 NULL字符并显式转换结果
  • 打印结果

按照上述步骤,我们可以实现给定句子/字符串的等效 ASCII 形式。

C++
// C++ implementation for converting
// a string into it's ASCII equivalent sentence
#include 
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


Javascript


输出:

ASCII Sentence:
7110110110711510211111471101101107115

转换成等价的 ASCII 语句的时间复杂度为O(len) ,其中 len 是字符串的长度。
应用:

  • 英语句子可以编码/解码成这种形式,例如将句子转换成等效的 ASCII 形式,并在每个数字上加 5,然后从编码器端发送。稍后,解码器可以从每个数字中减去 5 并将其解码为原始形式。这样只有发送者和接收者才能解码句子。
  • ASCII 形式也用于将数据从一台计算机传输到另一台计算机。

https://youtu.be/J8WGtK7I

-vi