How to resolve the algorithm Palindrome detection step by step in the Bash programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindrome detection step by step in the Bash programming language

Table of Contents

Problem Statement

A palindrome is a phrase which reads the same backward and forward. Write a function or program that checks whether a given sequence of characters (or, if you prefer, bytes) is a palindrome. For extra credit:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Palindrome detection step by step in the Bash programming language

Source code in the bash programming language

#! /bin/bash
# very simple way to detect a palindrome in Bash
# output of bash --version -> GNU bash, version 4.4.7(1)-release x86_64 ...

echo "enter a string"
read input

size=${#input}
count=0

while (($count < $size))
do
    array[$count]=${input:$count:1}
    (( count+=1 ))
done

count=0

for ((i=0 ; i < $size; i+=1))
do
    if [ "${array[$i]}" == "${array[$size - $i - 1]}" ]
    then
        (( count += 1 ))
    fi
done

if (( $count == $size ))
then
    echo "$input is a palindrome"
fi


  

You may also check:How to resolve the algorithm Array concatenation step by step in the bash programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Bash programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Bash programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Bash programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Bash programming language