เมื่อต้องการกลับลำดับสตริงโดยไม่ใช้ Recursion สามารถใช้ negative indexing ธรรมดาได้
Indexing ช่วยให้เข้าถึงองค์ประกอบที่ index เฉพาะได้
ตัวอย่าง
my_string = str(input("Enter a string that needs to be reversed: "))
print("The string after reversal is: ")
print(my_string[::-1])
ผลลัพธ์
Enter a string that needs to be reversed: Jane The string after reversal is: enaJ
คำอธิบาย
- รับ input สตริงจากผู้ใช้
- กำหนดค่าให้ตัวแปร
- แสดงบนคอนโซล
- ใช้ negative indexing และแสดงบนคอนโซล