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

给定一个包含构成句子的单词的字符串(属于英语)。任务是输出输入句子的等效ASCII句子。 ASCII格式的句子是对输入字符串的每个字符进行转换,并将其与字符串中的字符位置对齐 例如:

null
Input : hello, world!Output : ASCII Sentence:         104101108108111443211911111410810033Input : GeeksforGeeksOutput : ASCII Sentence:         7110110110711510211111471101101107115

说明: 为了完成这项任务,我们需要将每个字符转换成等价的字符 ASCII值 .我们执行以下步骤以获得给定句子的等效ASCII形式-

  • 重复整个句子/字符串的长度
  • 一次提取句子的每个字符,减去空字符,并显式输入结果
  • 打印结果

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

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 ( '' , 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:" . "" ;
ASCIISentence( $str );
// This code is contributed
// by ChitraNayal
?>


Javascript

<script>
// Javascript 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)
{
let l = str.length;
let convert;
for (let i = 0; i < l; i++) {
convert = str[i].charCodeAt(0);
document.write(convert);
}
}
// Driver function
let str = "GeeksforGeeks" ;
document.write( "ASCII Sentence:<br>" );
ASCIISentence(str);
// This code is contributed by rag2127
</script>


输出:

ASCII Sentence:7110110110711510211111471101101107115

转换为等效ASCII语句的时间复杂度为 O(len) ,其中len是字符串的长度。 申请:

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

https://youtu.be/J8WGtK7I

-六

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享