创建一个程序,要求用户输入单词。检查单词是否是回文。
当我键入LOL时,它说它不是回教堂。 PLS帮助我
DISPLAY "Enter a word to check if it is a Palindrome: "
ACCEPT WS-STR1
MOVE FUNCTION REVERSE(WS-STR1)TO WS-STR2
DISPLAY WS-STR1
DISPLAY WS-STR2
IF WS-STR1(1:1) = WS-STR2(15:1)
AND WS-STR1(2:1) = WS-STR2(14:1)
AND WS-STR1(3:1) = WS-STR2(13:1)
AND WS-STR1(4:1) = WS-STR2(12:1)
AND WS-STR1(5:1) = WS-STR2(11:1)
AND WS-STR1(6:1) = WS-STR2(10:1)
AND WS-STR1(7:1) = WS-STR2(9:1)
DISPLAY "A PALINDROME! "
ELSE
DISPLAY "NOT A PALINDROME "
END-IF.
答案 0 :(得分:2)
两件事。首先,如果您要反转字符串,请删除空格,否则它将永远不匹配。第二。反向回文等于自己,因此不需要逐字节比较字符串a =反向字符串a
DISPLAY "Enter a word to check if it is a Palindrome: "
ACCEPT WS-STR1
MOVE FUNCTION REVERSE(FUNCTION TRIM(WS-STR1)) TO WS-STR2
DISPLAY WS-STR1
DISPLAY WS-STR2
IF WS-STR1 = WS-STR2
DISPLAY "A PALINDROME! "
ELSE
DISPLAY "NOT A PALINDROME "
END-IF
STOP RUN.