Question:
Write a shell script to identify the given string is palindrome or not?
Answer:
Print("Please enter a string "); $input_string = <STDIN>; chop($input_string); $rev_string = reverse($input_string);
if($input_string eq $rev_string){ print("The string is a palindrome"); }else{ print("The string is NOT a palindrome"); } Source: CoolInterview.com
Check the script
1=0 cnt=1 tag=0 echo "Enter a String?" read str 1 =`echo $str |wc -c` 1 =`expr $l - 1` 1h=`expr $l / 2` while [ $cnt -le $lh ] do c1=`echo $str|cut -c$cnt` c2=`echo $str|cut -c$l` if [ $c1 != $c2 ] then cnt=$lh tag=1 fi cnt=`expr $cnt + 1` =`expr $l - 1` done if [ $tag -eq 0 ] then echo "String is Palindrome" else echo “String is not Palindrome” fi Source: CoolInterview.com
Answered by: Vijay | Date: 3/11/2009
| Contact Vijay
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|