answersLogoWhite

0

Shell script to check if a string is a palindrome?

Computer Programming Questions

Answers.com> http://wiki.answers.com/../ > http://wiki.answers.com/FAQ > http://wiki.answers.com/FAQ/3776> http://wiki.answers.com/FAQ/458> http://wiki.answers.com/FAQ/2096

View Slide Show

Best Answer

len=0

i=1

echo -n "Enter a String: "

read str

len=`echo $str | wc -c`

len=`expr $len - 1`

halfLen=`expr $len / 2`

while [ $i -le $halfLen ]

do

c1=`echo $str|cut -c$i`

c2=`echo $str|cut -c$len`

if [ $c1 != $c2 ] ; then

echo "string is not palindrome"

exit

fi

i=`expr $i + 1`

len=`expr $len - 1`

done

echo "String is Palindrome"

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


Why would you write a program using array processing linear search and binary search of an array?

A linear search is effective when the order of elements in an array are not sorted by the value you are looking for. A binary search is effective when the order of elements is sorted by the value you are looking for.Linear search sample data: 8 3 9 12 4 10 38 2 1 93 56 34Binary search sample data: 1 2 3 4 8 9 10 12 34 38 56 93In the first set of data, unsorted data cannot be searched using binary search. To find the value 38, a program must go through each element until it locates the 7th element; this is a total of 7 iterations. This method is effective when data is constantly being added and removed, and the overhead of a sorting algorithm would be less efficient than a binary search.In the second set of data, the value 38 can be found by binary search. In the first iteration, a binary halfway point is found (we will choose element 6). Since 9 is less than 38, we know we need to go up. There are six remaining values, so we look at the 9th element (starting from the 6th element, there are six more, so we go half-way, 3 more, a total of 9). Here, we see the value is 34, still less than 38. There are three values remaining, so we go up 2 more. For the third iteration, the value is 56, which is more than our target of 38. Since we advanced 2 last time, we will decrease by 1 this time, and our fourth iteration will find the value 38.As a matter of fact, in this data set, we will always find our answer in at most 4 iterations, while in the linear search, only the first 4 elements have a chance of being more efficient than the binary search. The problem then comes to down to if the sorting and binary search combined is faster than the linear search. For large data sets that are mostly static, binary searching is preferred. For rapidly changing data sets that would need constant sorting, a linear search may be preferred.Note that if the data insertion algorithm maintains the sort order (by inserting each element at the correct index in memory), binary searching will likely be faster in the majority of cases. One can use a binary search for data insertion points, keeping the cost of data insertion minimized (but not as efficient as simply appending to the end) while maximizing search capabilities.


Write an iterative function to search an element in a binary search tree?

_node* search (_node* head, _key key) { _node* node; for (node=head; node != NULL;;) { if (key == node->key) return node; else if (key < node.>key) node = node->left; else node = node->right; } return node; }


Write an algorithm to search an element in linked list?

To search for an element in a linked list, you iterate the list, looking for the element, and either return the element or an indication that it was not found. for (ptr = first; ptr != null; ptr = ptr.next) if (ptr.value == searchvalue) break; This will either leave ptr with the address of the found element, or null, if not found.

Related Questions

How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a program of binary heap in c or c language?

to implement operations on binary heap in c


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


Write a short note on linear search?

Linear search, also known as sequential search, is a process that checks every element in the list sequentially until the desired element is found. The computational complexity for linear search is O(n), making it generally much less efficient than binary search (O(log n)). But when list items can be arranged in order from greatest to least and the probabilities appear as geometric distribution (f (x)=(1-p) x-1p, x=1,2),then linear search can have the potential to be notably faster than binary search.


Write an awk program to search an element from an array?

Actually, if you use the awk language with associative arrays there is no need to search for an element of an array:val["abc"] = 2 ;will set the element 'abc' in the list 'val' to a value of 2. Since all values start out as blank or 0, finding if an element has a value is easy:if (val['abc'] == 2){# found the value}


Why would you write a program using array processing linear search and binary search of an array?

A linear search is effective when the order of elements in an array are not sorted by the value you are looking for. A binary search is effective when the order of elements is sorted by the value you are looking for.Linear search sample data: 8 3 9 12 4 10 38 2 1 93 56 34Binary search sample data: 1 2 3 4 8 9 10 12 34 38 56 93In the first set of data, unsorted data cannot be searched using binary search. To find the value 38, a program must go through each element until it locates the 7th element; this is a total of 7 iterations. This method is effective when data is constantly being added and removed, and the overhead of a sorting algorithm would be less efficient than a binary search.In the second set of data, the value 38 can be found by binary search. In the first iteration, a binary halfway point is found (we will choose element 6). Since 9 is less than 38, we know we need to go up. There are six remaining values, so we look at the 9th element (starting from the 6th element, there are six more, so we go half-way, 3 more, a total of 9). Here, we see the value is 34, still less than 38. There are three values remaining, so we go up 2 more. For the third iteration, the value is 56, which is more than our target of 38. Since we advanced 2 last time, we will decrease by 1 this time, and our fourth iteration will find the value 38.As a matter of fact, in this data set, we will always find our answer in at most 4 iterations, while in the linear search, only the first 4 elements have a chance of being more efficient than the binary search. The problem then comes to down to if the sorting and binary search combined is faster than the linear search. For large data sets that are mostly static, binary searching is preferred. For rapidly changing data sets that would need constant sorting, a linear search may be preferred.Note that if the data insertion algorithm maintains the sort order (by inserting each element at the correct index in memory), binary searching will likely be faster in the majority of cases. One can use a binary search for data insertion points, keeping the cost of data insertion minimized (but not as efficient as simply appending to the end) while maximizing search capabilities.


Write a program that takes a binary file as input and finds error check using different mechanism?

networking


Write an iterative function to search an element in a binary search tree?

_node* search (_node* head, _key key) { _node* node; for (node=head; node != NULL;;) { if (key == node->key) return node; else if (key < node.>key) node = node->left; else node = node->right; } return node; }


Write a c program to convert binary number to decimal number?

How is this a question? Sounds like you should do more of your homework offline.


Write a c program on reverse the diagonal element of matrix?

mano ni anda yarrr


How do you write 18 as binary?

Decimal 18 is 10010 in binary


How do you write 23 binary?

Decimal 23 is 10111 in binary