Docsity

Shell Scripting: Finding Palindromes and String Length, Lab Reports of Computer Networks

Toggle sidebar
bg1
EX NO:3 BASIC SHELL SCRIPTING KISHORE KUMAR V
DATE: 24.08.2020 1805090
AIM:
To write a shell script to find whether a string is palindrome or not and to find length of a string
without using inbuilt string.
1.SHELL SCRIPT PROGRAM TO CHECK WHETHER A STRING IS PALINDROME:
ALGORITHM:
Start the program
Let the user input a string
Checks each and every word from front and back
With the done statement, displays palindrome or not
Stop the program
PROGRAM CODE:
#!/bin/bash
echo "Enter a string to be entered:"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"
OUTPUT:
pf2

Partial preview of the text

Download Shell Scripting: Finding Palindromes and String Length and more Lab Reports Computer Networks in PDF only on Docsity!

EX NO:3 BASIC SHELL SCRIPTING KISHORE KUMAR V

DATE: 24.08.2020 1805090

AIM:

To write a shell script to find whether a string is palindrome or not and to find length of a string without using inbuilt string. 1.SHELL SCRIPT PROGRAM TO CHECK WHETHER A STRING IS PALINDROME: ALGORITHM:  Start the program  Let the user input a string  Checks each and every word from front and back  With the done statement, displays palindrome or not  Stop the program PROGRAM CODE: #!/bin/bash echo "Enter a string to be entered:" read str echo len=echo $str | wc -c len=expr $len - 1 i= j=expr $len / 2 while test $i -le $j do k=echo $str | cut -c $i l=echo $str | cut -c $len if test $k != $l then echo "String is not palindrome" exit fi i=expr $i + 1 len=expr $len - 1 done echo "String is palindrome" OUTPUT:

2.CALCULATE STRING LENGTH WITHOUT USING INBUILT FUNCTION: ALGORITHM:  Start the program  Let the user input a string  Counts each and every statement  With the done statement, display the length  Stop the program SOURCE CODE: !#/bin/bash echo “Enter the string:” read str i= val= len= IFS= ‘’ read –a strarr <<< “$str” echo “There are ${#strarr[*]} words in the text” while [ “$i” –lt “${#str}” ]; do val= “${str:$i:1}” len = $(( len+1 )) i=$(( i+1 )) done echo “The length of the string is: $len” OUTPUT: RESULT: Thus the case study on the operators, looping statements and conditional statements have done, and applied the insights in executing the programs.