qq_56929944 2021-12-16 20:22 采纳率: 100%
浏览 315
已结题

Python如何用栈测试输入字符串判断是否是回文

Python如何用栈测试输入字符串判断是否是回文

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-12-16 20:25
    关注
    # Python program to check if a string is 
    # palindrome or not
     
    # function to check palindrome string
    def isPalindrome(string):
      result = True
      str_len = len(string)
      half_len= int(str_len/2)
     
      for i in range(0, half_len):
        # you need to check only half of the string
        if string[i] != string[str_len-i-1]:
          result = False
        break
      
      return result 
     
    # Main code
    x = "Google"
    if isPalindrome(x):
      print(x,"is a palindrome string")
    else:
      print(x,"is not a palindrome string")  
     
    x = "ABCDCBA"
    if isPalindrome(x):
      print(x,"is a palindrome string")
    else:
      print(x,"is not a palindrome string")
     
    x = "RADAR"
    if isPalindrome(x):
      print(x,"is a palindrome string")
    else:
      print(x,"is not a palindrome string") 
    
    
    
    

    https://blog.csdn.net/cumt951045/article/details/107766926

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月29日
  • 已采纳回答 12月21日
  • 创建了问题 12月16日