0

Hello i am having an error when i try to compile my code.

int main()
{

char string_buffer[20], string_buffer1[20];//which is going to be copied into and reversed.

printf("Enter the string to check if it is a palindrome\n");
scanf("%20s", string_buffer);

strcpy(string_buffer1,string_buffer);//copying string_buffer into string_buffer1
strrev(string_buffer1);//reverse string_buffer1

if (strcmp(string_buffer,string_buffer1) == 0){//check to see if they are the same
    printf("Palindrome.\n");

}else{
    printf("Not a palindrome.\n");

}       
    return 0;

}

When i try to compile i get this warning and error.

palindrome.c:12:2: warning: implicit declaration of function 'strrev' is invalid
      in C99 [-Wimplicit-function-declaration]
        strrev(string_buffer1);//reverse string_buffer1
        ^
1 warning generated.
/tmp/palindrome-1efe10.o: In function `main':
palindrome.c:(.text+0x68): undefined reference to `strrev'
clang-3.5: error: linker command failed with exit code 1 (use -v to see invocation)
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • I don't think `strrev` is available in linux. – Haris Jan 14 '16 at 12:56
  • 2
    I believe this could be considered a duplicate to this. http://stackoverflow.com/questions/8534274/is-the-strrev-function-not-available-in-linux – arduic Jan 14 '16 at 12:57
  • Possible duplicate of [Palindrome program in C](http://stackoverflow.com/questions/34784096/palindrome-program-in-c) – Box Box Box Box Jan 14 '16 at 13:01
  • 2
    Possible duplicate of [Echo All Palindromes, in C](http://stackoverflow.com/questions/3793498/echo-all-palindromes-in-c) – Rob Erskine Jan 14 '16 at 13:25

2 Answers2

0

You don't have strrev in linux, use one of the alternatives (taken from here):

#include <string.h>

char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;
}
Idos
  • 15,053
  • 14
  • 60
  • 75
0

You don't need strrev or even require to duplicate the memory. The following code is a much simpler and a direct answer to your question. Please note that this code assumes that neither NULL nor "" are palindroms. The function returns 1 if the passed string is a palindrome and 0 otherwise.

int is_palindrome(const char *str)
{
    size_t len = str ? strlen(str) : 0;
    const char *p;

    if (len < 1)
            return 0;

    for (p = str + len - 1; str < p; str++, p--)
            if (*p != *str)
                    return 0;
    return 1;
}
gollum
  • 2,472
  • 1
  • 18
  • 21