gpt4 book ai didi

java - Java 中的凯撒密码(西类牙语字符)

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:28 25 4
gpt4 key购买 nike

我已阅读this question ,我想知道是否有办法考虑整个字符范围?例如“á”、“é”、“ö”、“ñ”,而不考虑“”([空格])? (例如,我的字符串是“Hello World”,标准结果是“Khoor#Zruog”;我想删除那个“#”,所以结果将是“KhoorZruog”)

我确信我的答案就在这段代码中:

if (c >= 32 && c <= 127)
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - 32;
x = (x + shift) % 96;
chars[i] = (char) (x + 32);
}

但是我尝试了一些方法,但没有成功。

最佳答案

请参阅此伪代码 - 应该可以轻松实现:

// you need to define your own range, obviously - it's not at all obvious whether
// e.g. "ź" should be included and that it should come after "z"
array char_range = ['a','á','b','c','č', (...), 'z','ź','ž']
// the text to encode
string plaintext = 'some text here'
// this will contain encoded text
stringbuilder ciphertext = ''
// the classic Caesar Cipher shifts by 3 chars to the right
// to decipher, reverse the sign
int shift_by = 3
// note: character != byte, esp. not in UTF-8 (1 char could be 1 or more bytes)
for each character in plaintext
get character_position of character in char_range // e.g. "a" would return 0
if not in char_range // e.g. spaces and other non-letters
do nothing // drop character
// alternately, you can append it to ciphertext unmodified
continue with next character
add shift_by to character_position
if character_position > char_range.length
character_position modulo char_range.length
if character_position < 0 // useful for decoding
add char_range.length to character_position
get new_character at character_position
append new_character to ciphertext
done

关于java - Java 中的凯撒密码(西类牙语字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3037195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com