1

I'm trying to reverse a string using the function strrev(). I know that strrev returns a pointer to the reversed string so I simply initialize an already allocated string with same size as the original one with the strrev function return. Obviously this isn't the correct way to do it and I get an "incompatible types" error in that line.

Here's the code:

int ispalindrome(int n)
{
   char s[10], sr[10];

   itoa(n, s, 10);

   printf("%s", s);

   sr = strrev(s);

   printf("\nReverse: %s", sr);

   if(strcmp(s, sr) == 0)
       return 1;

   else
       return 0;

}
Arlind
  • 436
  • 2
  • 9
  • 22
  • `itoa` is a nonstandard function, suggested function to use is `sprintf` as per http://www.cplusplus.com/reference/cstdlib/itoa/ – unxnut Jun 05 '13 at 17:42

2 Answers2

7
sr[10];
sr = strrev(s);

This doesn't even compile - arrays are not assignable. Post real code.

(You need to declare sr as char *sr for this to actually compile at all.)


Apart from that, your issue is that strrev() reverses the string in place, so the two strings will always compare equal (since you're effectively comparing the reversed string with itself). What you have to do is:

  • superfluously inefficient way: create a copy of the string, strrev() that, then strcmp() the original and the copy.

  • Somewhat more optimized approach for non-empty strings:


int ispal(const char *s)
{
    const char *p = s + strlen(s) - 1;
    while (s < p)
        if (*p-- != *s++)
            return 0;

    return 1;
}
  • 2
    I think the question was about the code not compiling. Well, part of it anyway, I didn't really see a `?` anywhere. – Bernhard Barker Jun 05 '13 at 17:34
  • I liked the code snippet, even though it was not an answer to the original question. – unxnut Jun 05 '13 at 17:41
  • 1
    This code is not correct if you pass in the empty string... it exhibits undefined behavior. – Paul Hankin Jun 05 '13 at 18:45
  • 1
    @H2CO3 when s is "", p will point to s-1. See the excellent first answer to http://stackoverflow.com/questions/16234626/pointer-comparisons-with-one-before-the-first-element-of-an-array-object why this is undefined behavior. – Paul Hankin Jun 05 '13 at 20:12
  • @Anonymous Ah, off-by-one. Fixed. –  Jun 05 '13 at 20:14
  • Nope, in the case that `strlen(s) == 0` then `p = s-1`, and `while (s < p)` gives undefined behavior because p isn't valid. – Paul Hankin Jun 05 '13 at 20:19
  • @Anonymous That's what I'm saying. I've read the answer you linked. –  Jun 05 '13 at 20:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31283/discussion-between-anonymous-and-h2co3) – Paul Hankin Jun 05 '13 at 20:24
  • @H2CO3 Anonymous is right.you could either introduce a null string check or do this `const char *p = s + strlen(s)` and then `if(*--p != *s++)`. – Koushik Shetty Jun 06 '13 at 08:04
  • @Koushik I've included "for non-empty strings" in the answer, and that's enough. I'm not interested in discussing this point further. –  Jun 06 '13 at 08:16
2

OK, did some research and looks like strrev is not available in Linux (if that is your platform); check out Is the strrev() function not available in Linux?

You can use the alternative implementation suggested therein or use the answer by @H2CO3.

Community
  • 1
  • 1
unxnut
  • 8,509
  • 3
  • 27
  • 41