-1

Following is the C program I wrote to check whether Entered string is Palindrome or not, but it always displays the 'else' statement i.e. The string is not Palindrome:-

#include<stdio.h>
#include<string.h>
void main()
{
int i,n,count=0;
char f[30];
printf("Enter the string. :  ");
gets(f);
n = strlen(f);

for(i=0;i<n;i++)
{
    if(f[i+1]==f[n-i])
    count=count+1;
}
if(count==n)
printf("\n Entered string is Palindrome");
else
printf("\n Entered string is NOT Palindrome");

}
  • 1
    Welcome to Stack Overflow. You should use print statements or a debugger to look at the values in `n` and `count`. Compare them with your expected values. – Bill the Lizard Sep 11 '18 at 15:31
  • 1
    First of all you should stop using `gets`. [It's a dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used), and has therefore been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Sep 11 '18 at 15:31
  • 3
    https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. And your program _is_ really small. – Jabberwocky Sep 11 '18 at 15:31
  • Are you sure your program *always* displays the 'else' statement? Try one-char input. – MikeCAT Sep 11 '18 at 15:32
  • Thank you all for all the answers, count always shows 1. – Vengence Tech Sep 11 '18 at 15:34
  • @VengenceTech "count always shows 1" really? I tested with an input `ab` and got a result `count = 0` by adding `printf("%d\n", count);` just before `if(count==n)`. – MikeCAT Sep 11 '18 at 15:36

2 Answers2

0

When i = 0, f[n-i] will be the terminating null character, which will never appear in the middle of the string. Because of that, if the string is 2-char long or more, the condition f[i+1]==f[n-i] will be false. (If the string is 1-char long, f[i+1] will be the terminating null character after the first (and only) character, so the condition will be true.)

The condition should be f[i]==f[n-i-1].

By the way,

  • You shouldn't use gets(), which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11.
  • You should use standard int main(void) in hosted environment instead of void main(), which is illegal in C89 and implementation-defined in C99 or later, unless you have some special reason to use this non-standard signature (for example, forced to use this from your boss or teacher).

An example of full fixed code:

#include<stdio.h>
#include<string.h>
int main(void)
{
    int i,n,count=0;
    char f[30 + 1]; /* allocate one more element for storeing newline character */
    char* lf;
    printf("Enter the string. :  ");
    fgets(f, sizeof(f), stdin); /* change gets() to fgets() */
    /* fgets() stores newline character while gets() doesn't, so remove it */
    if ((lf = strchr(f, '\n')) != NULL) *lf = '\0';
    n = strlen(f);

    for(i=0;i<n;i++)
    {
        if(f[i]==f[n-i-1])
        count=count+1;
    }
    if(count==n)
        printf("\n Entered string is Palindrome");
    else
        printf("\n Entered string is NOT Palindrome");

}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thank you so much for the answer, Here we are forced to use Borland compiler which suggests Education system here is outdated and they dont intend to change. – Vengence Tech Sep 11 '18 at 15:42
0

I think just the indexing in the string is wrong. Change it from i+1 to i and n-i-2

#include<stdio.h>
#include<string.h>
void main()
{
    int i,n,count=0;
    char f[30];
    printf("Enter the string. :  ");
    fgets(f, 29, stdin);
    n = strlen(f);

    for(i=0;i<n;i++)
    {
        if(f[i]==f[n-i-2])
        count=count+1;
    }
    if(count==n)
    printf("\n Entered string is Palindrome");
    else
    printf("\n Entered string is NOT Palindrome");

}

Also another more efficient should would be:

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

void main()
{
    int i = 0,n,count=0;
    char f[30];
    printf("Enter the string. :  ");
    fgets(f, 29, stdin);
    n = strlen(f);

    while (i < n >> 1) {
        if (f[i]!=f[n-i-2]) {
            printf("\n Entered string is NOT Palindrome\n");
            return;
        }
        i++;
    }
    printf("\n Entered string is Palindrome\n");
    return;
}