DEV Community

Fahad Vahora
Fahad Vahora

Posted on

Linux Practical Set C Solution – Palindrome String

In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set C using shell scripting.

We will learn how to check whether a given string is a palindrome or not using different shell scripting approaches.


📌 Practical Question

Write a script to check whether a given string is palindrome or not.


🧠 What is a Palindrome?

A palindrome is a word or string that reads the same from left to right and right to left.

Examples

madam
level
radar
racecar
Enter fullscreen mode Exit fullscreen mode

For example:

madam → madam
Enter fullscreen mode Exit fullscreen mode

Since the original string and reversed string are the same, madam is a palindrome.

Non-Palindrome Examples

hello
linux
computer
Enter fullscreen mode Exit fullscreen mode

For example:

hello → olleh
Enter fullscreen mode Exit fullscreen mode

Since they are different, hello is not a palindrome.


✅ Solution 1: Using for Loop

This solution checks the string character by character and builds its reverse.

Shell Script

#!/bin/bash

echo "Enter a string:"
read str

reverse=""

for ((i=${#str}-1; i>=0; i--))
do
    ch=${str:$i:1}
    reverse="$reverse$ch"
done

echo "Original String : $str"
echo "Reverse String  : $reverse"

if [ "$str" = "$reverse" ]
then
    echo "String is Palindrome"
else
    echo "String is Not Palindrome"
fi
Enter fullscreen mode Exit fullscreen mode

🔍 Explanation

Step 1: Take Input

read str
Enter fullscreen mode Exit fullscreen mode

The user-entered string is stored in the variable str.


Step 2: Initialize Reverse String

reverse=""
Enter fullscreen mode Exit fullscreen mode

We start with an empty string.

Characters will be added to this variable one by one.


Step 3: Find String Length

${#str}
Enter fullscreen mode Exit fullscreen mode

This returns the number of characters in the string.

For example:

madam
Enter fullscreen mode Exit fullscreen mode

has:

5 characters
Enter fullscreen mode Exit fullscreen mode

Therefore, the last character is at index:

4
Enter fullscreen mode Exit fullscreen mode

because Bash string indexing starts from 0.


Step 4: Loop from Last Character to First

for ((i=${#str}-1; i>=0; i--))
Enter fullscreen mode Exit fullscreen mode

For:

madam
Enter fullscreen mode Exit fullscreen mode

the indexes are:

m → 0
a → 1
d → 2
a → 3
m → 4
Enter fullscreen mode Exit fullscreen mode

The loop starts from 4 and moves toward 0.

So the characters are read as:

m
a
d
a
m
Enter fullscreen mode Exit fullscreen mode

Step 5: Extract One Character

ch=${str:$i:1}
Enter fullscreen mode Exit fullscreen mode

This extracts one character from the string.

The general syntax is:

${string:start:length}
Enter fullscreen mode Exit fullscreen mode

For example:

${str:2:1}
Enter fullscreen mode Exit fullscreen mode

means:

Start at index 2 and extract 1 character.


Step 6: Build the Reverse

reverse="$reverse$ch"
Enter fullscreen mode Exit fullscreen mode

Each extracted character is added to the reverse variable.

For example:

Original: hello

Reverse building:

o
ol
oll
olle
olleh
Enter fullscreen mode Exit fullscreen mode

Step 7: Compare Strings

if [ "$str" = "$reverse" ]
Enter fullscreen mode Exit fullscreen mode

If the original string and reversed string are equal, the string is a palindrome.


🖥️ Example Output

Example 1

Enter a string:
madam

Original String : madam
Reverse String  : madam
String is Palindrome
Enter fullscreen mode Exit fullscreen mode

Example 2

Enter a string:
hello

Original String : hello
Reverse String  : olleh
String is Not Palindrome
Enter fullscreen mode Exit fullscreen mode

✅ Solution 2: Using rev Command

Linux provides a command called rev that reverses characters in each line.

This makes the palindrome program much shorter.

Shell Script

#!/bin/bash

echo "Enter a string:"
read str

reverse=$(echo "$str" | rev)

echo "Original String : $str"
echo "Reverse String  : $reverse"

if [ "$str" = "$reverse" ]
then
    echo "String is Palindrome"
else
    echo "String is Not Palindrome"
fi
Enter fullscreen mode Exit fullscreen mode

🔍 Understanding rev

Consider:

echo "madam" | rev
Enter fullscreen mode Exit fullscreen mode

Output:

madam
Enter fullscreen mode Exit fullscreen mode

And:

echo "hello" | rev
Enter fullscreen mode Exit fullscreen mode

Output:

olleh
Enter fullscreen mode Exit fullscreen mode

Therefore:

reverse=$(echo "$str" | rev)
Enter fullscreen mode Exit fullscreen mode

stores the reversed string in the reverse variable.

Then we simply compare:

if [ "$str" = "$reverse" ]
Enter fullscreen mode Exit fullscreen mode

🖥️ Example Output

Enter a string:
level

Original String : level
Reverse String  : level
String is Palindrome
Enter fullscreen mode Exit fullscreen mode

✅ Solution 3: Character-by-Character Comparison

Instead of creating a separate reverse string, we can compare characters from both ends.

Shell Script

#!/bin/bash

echo "Enter a string:"
read str

length=${#str}
isPalindrome=1

for ((i=0, j=length-1; i<j; i++, j--))
do
    if [ "${str:$i:1}" != "${str:$j:1}" ]
    then
        isPalindrome=0
        break
    fi
done

if [ $isPalindrome -eq 1 ]
then
    echo "String is Palindrome"
else
    echo "String is Not Palindrome"
fi
Enter fullscreen mode Exit fullscreen mode

🔍 How This Solution Works

Suppose the input is:

madam
Enter fullscreen mode Exit fullscreen mode

The script compares:

m ↔ m
a ↔ a
d ↔ d
Enter fullscreen mode Exit fullscreen mode

All characters match, so the string is a palindrome.

For:

hello
Enter fullscreen mode Exit fullscreen mode

the first comparison is:

h ↔ o
Enter fullscreen mode Exit fullscreen mode

They are different, so the script immediately stops.

break
Enter fullscreen mode Exit fullscreen mode

This avoids unnecessary comparisons.


📊 Comparing the Solutions

Method Difficulty Extra Command Good for Practical
for + Reverse ⭐⭐ No ✅ Excellent
rev ⭐ Easy rev ✅ Excellent
Character Comparison ⭐⭐⭐ No ✅ Good

Which one should you use?

For a practical examination:

Beginner-friendly: Use the rev solution.

To demonstrate shell scripting logic: Use the for loop solution.

For better algorithmic understanding: Use character-by-character comparison.


📝 Quick Revision

Reverse using rev

reverse=$(echo "$str" | rev)
Enter fullscreen mode Exit fullscreen mode

String length

${#str}
Enter fullscreen mode Exit fullscreen mode

Extract one character

${str:$i:1}
Enter fullscreen mode Exit fullscreen mode

Compare strings

if [ "$str" = "$reverse" ]
Enter fullscreen mode Exit fullscreen mode

Break the loop

break
Enter fullscreen mode Exit fullscreen mode

⚠️ Important Note About Spaces and Case

The basic solutions above treat the input as a normal string.

For example:

Madam
Enter fullscreen mode Exit fullscreen mode

is not considered the same as:

madam
Enter fullscreen mode Exit fullscreen mode

because uppercase and lowercase characters are different.

Similarly:

nurses run
Enter fullscreen mode Exit fullscreen mode

contains a space, so it is different from:

nursesrun
Enter fullscreen mode Exit fullscreen mode

If the practical specifically requires case-insensitive palindrome checking or ignoring spaces, the script needs a small modification.


🎯 Practical Exam Tips

Remember these important points:

  1. A palindrome is the same when read forward and backward.
  2. Bash string indexes start from 0.
  3. ${#str} gives the string length.
  4. ${str:$i:1} extracts a character.
  5. rev can reverse a string quickly.
  6. Always quote string variables when comparing them.
  7. break can stop the loop as soon as a mismatch is found.

📚 Related Linux Practical Sets

This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 solution series.

  • Set A – Simple Interest & Compound Interest
  • Set B – Vowels Count & Case Conversion
  • Set C – Palindrome String
  • Set D – File Operations & Text Processing
  • Set E – Display Lines with Validation
  • Set F – Count Files & Directories
  • Set G – Recently Modified Files
  • Set H – Employee Gross Salary

🐧 Conclusion

The Palindrome String practical is a simple but important shell scripting exercise.

It helps students understand:

  • Strings
  • Loops
  • String indexing
  • Conditions
  • Command substitution
  • Linux commands

Try writing the program yourself using the for loop approach first. After understanding the logic, try the shorter rev version.

Happy Learning & Best of Luck for your Linux Practical! 🐧💻

Top comments (0)