Caesar cipher · รหัสซีซาร์
Caesar cipher
The Roman historian Suetonius wrote that Julius Caesar hid his messages by shifting each letter three places along the alphabet.
alphabet.index(ch) gives a letter's position, from 0 for a to 25 for z. Add the shift and use % 26, so that position 26 wraps back to 0. To decode a message, shift it again by 26 minus the original shift.
Caesar cipher
นักประวัติศาสตร์โรมันซูเอตอเนียสเขียนว่าจูลิซ ซีซาร์ซ่อนข้อความโดยการเลื่อนแต่ละตัวอักษรไปสามตำแหน่งตามลำดับอักษร
alphabet.index(ch) แสดงตำแหน่งของตัวอักษร โดย 0 สำหรับ a ไปจนถึง 25 สำหรับ z เพิ่มค่าเลื่อนและใช้ % 26 เพื่อทำให้ตำแหน่ง 26 หมุนกลับมาที่ 0 ในการถอดรหัสข้อความ ให้เลื่อนอีกครั้งโดย 26 ลบด้วยค่าเลื่อนเริ่มต้น
Write caesar(text, shift) that moves every lowercase letter shift places along the alphabet, wrapping from z back to a. Leave every other character as it is. caesar("hello, world", 3) is "khoor, zruog". · เขียน caesar(text, shift) ที่เลื่อนตัวอักษรพิมพ์เล็กทุกตัว shift ตำแหน่งในอัลファเบต จาก z กลับไปที่ a. ทิ้งทุกอักขระอื่นไว้เท่าเดิม. caesar("hello, world", 3) คือ "khoor, zruog"
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่