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 Apr 2017
  1. akash.dinkar12

    3366 Points

  2. Divya Bharti

    2536 Points

  3. Deepthi_ts

    2040 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Shubham Sharma 2

    1610 Points

  7. Debashish Deka

    1584 Points

  8. Prashant.

    1462 Points

  9. Arunav Khare

    1444 Points

  10. Kapil

    1414 Points

Monthly Topper: Rs. 500 gift card

22,072 questions
28,030 answers
63,193 comments
24,128 users