gpt4 book ai didi

linux - 检查一个字符串是否是回文

转载 作者:太空狗 更新时间:2023-10-29 11:29:17 25 4
gpt4 key购买 nike

我正在尝试检查一个字符串是否是 bash 中的回文。这是我想出的:

#!/bin/bash
read -p "Enter a string: " string
if [[ $string|rev == $string ]]; then
echo "Palindrome"
fi

现在,echo $string|rev 给出反转的字符串。我的逻辑是在 if 的条件下使用它。结果不太好。

那么,如何将 rev 的“返回值”存储到变量中呢? 还是直接在条件中使用?

最佳答案

没有 echo[[ ... ]] 中不必要的引号的另一种变体:

#!/bin/bash
read -p "Enter a string: " string
if [[ $(rev <<< "$string") == "$string" ]]; then
echo Palindrome
fi

关于linux - 检查一个字符串是否是回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743049/

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