-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpalindrome_checker.sh
More file actions
executable file
·54 lines (44 loc) · 1.56 KB
/
palindrome_checker.sh
File metadata and controls
executable file
·54 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Function to check if a word is a palindrome
is_palindrome() {
# Get the input word
local original_input=$1
local word=$1
# Convert to lowercase to make the check case-insensitive
word=$(echo "$word" | tr '[:upper:]' '[:lower:]')
# Remove non-alphanumeric characters
cleaned_word=$(echo "$word" | tr -cd '[:alnum:]')
# Get the reversed word
local reversed=""
local length=${#cleaned_word}
for (( i=$length-1; i>=0; i-- )); do
reversed="$reversed${cleaned_word:$i:1}"
done
# Compare the original and reversed words
if [ "$cleaned_word" = "$reversed" ]; then
echo "\"$original_input\" is a palindrome!"
echo "When we remove the spaces and punctuation, it will read the same forward and backward."
echo "Forward: $cleaned_word"
echo "Backward: $reversed"
else
echo "\"$original_input\" is not a palindrome."
echo "It doesn't read the same forward and backward."
echo "Forward: $cleaned_word"
echo "Backward: $reversed"
fi
}
# Welcome message
echo "=== Palindrome Checker ==="
echo "Palindrome: A word or phrase reads the same forward and backward."
echo "Examples: 'radar', 'madam', 'racecar', etc"
echo
# Ask for user input
echo "Please enter a word or phrase to check:"
read user_input
# Check if input is empty and figure out how to put in a loop
if [ -z "$user_input" ]; then
echo "No input provided. Please try again."
exit 1
fi
# Call the palindrome function with user input
is_palindrome "$user_input"