0

output expression is not correct

code :

#include <stdio.h>
#include <string.h>

int main() {
    char a[40],b[40];
    printf("character input: "); fgets(a, sizeof(a), stdin);
    strcpy(b, a);
    strrev(a);
    printf("%s\n", a);
    printf("%s", b);
    
    if (b == a) printf("palindrome");
    else printf("not palindrome");
}

character input:

seles

output:

seles
seles
not palindrome
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
grant
  • 23
  • 1
  • 1
    C and C++ are different languages. You can compile this as C++ (I think), but in idomatic C++ almost every single line would be different. Please only tag the language you are using – 463035818_is_not_an_ai Mar 16 '23 at 09:18
  • 2
    In the comparison `b == a`, both arrays will decay to *pointers* to their first elements. Since `a` and `b` are two different arrays located at different places in memory, the pointers will never by equal. Your beginners or learning material should have the information you need to compare strings. – Some programmer dude Mar 16 '23 at 09:19

3 Answers3

1

The function fgets can append to the entered string the new line character '\n' that corresponds to the pressed key Enter provided that the destination character array has space to store it.

You need to remove it like for example

a[ strcspn( a, "\n" ) ] = '\0';

before reversing the string.

Also in this if statement

if (b == a) printf("palindrome");

there are compared two pointers that point to first characters of the strings instead of the strings themselves. Instead you need to write using standard C function strcmp

if ( strcmp( b, a ) == 0 ) puts("palindrome");
else puts("not palindrome");
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I think you can use like this:

if (strcmp(a, b) == 0) printf("Palindrome\n");
else printf("Not palindrome\n");
0
  1. strrev is not a standard c function. It is not available on all compilers
  2. You will have to traverse through entire string matching individual characters or use strcmp to check if both strings are same.
  3. With fgets, you also read newline character. This newline character needs to be filtered out a[strcspn(a, "\n")] = 0

Code :

#include <stdio.h>
#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;
}

int main() {
    char a[40],b[40];
    memset(a, 0, 40);
    memset(b, 0, 40);
    printf("character input: ");
    fgets(a, sizeof(a), stdin);
    a[strcspn(a, "\n")] = 0; //Remove newline from buffer
    strcpy(b, a);
    strrev(a);
    printf("%s\n", a);
    printf("%s\n", b);
    for(int i = 0; i < strlen(a); i++)
    {
        if (b[i] != a[i]) 
        {
            printf("not palindrome");
            return 0;
        }
    }
    printf("palindrome");
}
Dark Sorrow
  • 1,681
  • 14
  • 37
  • Why `for(int i = 0; i < strlen(a); i++) { if (b[i] != a[i]) ... }` instead of `strcmp()`? – chux - Reinstate Monica Mar 16 '23 at 09:32
  • As I mentioned in my post both can work. – Dark Sorrow Mar 16 '23 at 09:34
  • Why `memset(a, 0, 40);`? The buffer(s) need not be initialised before being filled by `fgets()` and `strcpy()`.. – Fe2O3 Mar 16 '23 at 09:34
  • 1
    @DarkSorrow If you want to zero fill buffers first (needed or not), rather than `memset(a, 0, 40);` _after_ the definition is done, consider `char a[40] = { 0 };` to _initialize_ to zero as part of the definition. – chux - Reinstate Monica Mar 16 '23 at 09:41
  • @chux-ReinstateMonica Is their a performance benefit for using `char a[40] = { 0 };` over `memset(a, 0, 40);` or is it only for aesthetical purpose. – Dark Sorrow Mar 16 '23 at 09:58
  • 1
    @DarkSorrow It is not only for aesthetical purpose. Advantages include: 1) `a[]` immediately has a value and not at some later time. See [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) 2) Slightly easier for a compiler to optimize out when possible and the optimizer does not need to "know" `memset()` details. 3) Less maintenance. Curious you did not code `memset(a, 0, sizeof a);`. 4) does not require `#include `. 5) Less source code. – chux - Reinstate Monica Mar 16 '23 at 10:07
  • @DarkSorrow, what advantages do you see with `char a[40]; memset(a, 0, 40);`? – chux - Reinstate Monica Mar 16 '23 at 10:09
  • @chux-ReinstateMonica I don't see any advantages as such. It was just that lot of legacy code I worked on use explicit `memset` after declaring buffers. – Dark Sorrow Mar 16 '23 at 10:14
  • 1
    @DarkSorrow You may want to review [XOR technique](https://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice). – chux - Reinstate Monica Mar 16 '23 at 10:18