BASH Palindrome Checker

前端 未结 5 1951
[愿得一人]
[愿得一人] 2021-01-24 19:14

This is my first time posting on here so bear with me please.

I received a bash assignment but my professor is completely unhelpful and so are his notes.

Our assig

5条回答
  •  情深已故
    2021-01-24 20:01

    Like this:

    for word in `grep -E '^[a-z]{3,45}$' /usr/share/dict/words`;
        do [ $word == `echo $word | rev` ] && echo $word;
    done;
    

    Output using my dictionary:

    aha
    bib
    bob
    boob
    ...
    wow
    

    Update

    As pointed out in the comments, reading in most of the dictionary into a variable in the for loop might not be the most efficient, and risks triggering errors in some shells. Here's an updated version:

    grep -E '^[a-z]{3,45}$' /usr/share/dict/words | while read -r word;
        do [ $word == `echo $word | rev` ] && echo $word;
    done;
    

提交回复
热议问题