GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
63 views

What is the functionality of above function Do ( ) ?
a. Check whether string is odd palindrome
b. Check whether the string is even palindrome
c.  Check whether the string is palindrome
d.  None of the above

asked in Programming by Active (1.3k points)   | 63 views
think c is the answer

1 Answer

+3 votes
Best answer
Edit:

The correct answer would be D) none.

The reason is $gate1$ and $gate$ both point to the first character. So loop condition $gate1 < gate$ is already false and hence the loop breaks and the function returns 1.

The original answer was option C) by assuming (erroneously) that the while loop condition is $gate1 < gate2$, whereas the condition is $gate1 < gate$
answered by Loyal (3.3k points)  
edited by
how???

Edit:  The following discussion assumes wrong loop condition. So the code in the question actually always returns 1.

gate1 points to the first character of the string (base address of the string) and gate2 points to the last element.

The following part compares the current characters, being pointed by gate1 and gate2, if they're not equal straightaway return 0 (i.e., not a palindrome).

while(gate1 < gate2){
        if(*gate1++ != *gate2--)
            return 0;
    }

 *gate1++ : this means first increment gate1, but since it is post increment, first dereference the pointer, then increment it to point the next character.

 *gate2-- : this means first decrement gate2, but since it is post decrement, first dereference the pointer, then decrement it to point the previous character.

So the "if" condition first compares the current character, if they match, changes the corresponding pointers to point to their corresponding next elements.

If the whole loop ran without returning 0 (i.e.till the middle of the string) then return 1. So it checks if the string is palindrome or not.

thanks :)

Edit:  The following discussion assumes wrong loop condition. So the code in the question actually always returns 1.

@Gabbar: It doesn't need to. Whatever middle element be, the string will be a palindrome if the string by removing the middle element is a palindrome.

Consider $aba$, gate1 points at first $a$, gate2 points at last $a$

First Run:

Loop condition: $gate1 < gate2$ satisfied

Inside loop: $if$ condition satisfied. Increment gate1 and decrement gate2. Now both point at b.

Second Run:

Loop condition: $fails$, so exit out of loop and return 1.

Point to note is whatever the middle element be (one character long), the string $a\_a$ (consider the underscore a wildcard for single character)  will always be a palindrome.

oops !

thanks


Top Users May 2017
  1. akash.dinkar12

    3568 Points

  2. pawan kumarln

    2206 Points

  3. Bikram

    1940 Points

  4. sh!va

    1682 Points

  5. Arjun

    1650 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1270 Points

  8. Angkit

    1056 Points

  9. LeenSharma

    1028 Points

  10. Arnab Bhadra

    904 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    1026 Points

  2. pawan kumarln

    832 Points

  3. Arnab Bhadra

    818 Points

  4. akash.dinkar12

    448 Points

  5. Arjun

    378 Points


22,897 questions
29,213 answers
65,336 comments
27,713 users