Nisha Vittal
Nisha Vittal

Reputation: 13

Palindrome of string in c without using library functions

I actually created the code for palindrome of string using reverse and copy of strings without library function.But still it shows always it is palindrome.What mistake I have done.And it always prints it is always a palindrome regardless whether I give palindrome or not.I checked internet for code but want to know what mistake I made in my code so please help.

#include <stdio.h>
#include <string.h>
int main()
{
    char s[1000];
    char t[1000],temp;
    int i,j,flag=0;

    gets(s);

    for(i=0;i<strlen(s);i++)
    {
        t[i]=s[i];
    }
    printf("%s",t);
    int n=strlen(s)-1;
    for(j=0,i=n;i>=0;j++,i--)
    {
        t[j]=s[i];
    }
    printf("%s",t);

    for(i=0,j=0;i<=n,j<=n;i++,j++)
    {
        //for(j=0;j<n;j++){
        if(t[j]==t[i])
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
    {
        printf(" palindrome");
    }
    else
    {
        printf("else Palindrome");
    }
    return 0;
}

Upvotes: 0

Views: 6239

Answers (3)

yobro97
yobro97

Reputation: 1145

Irrespective of the rest of the code, what I could not understand is:

for(i=0,j=0;i<=n,j<=n;i++,j++)
{
    if(t[j]==t[i])//here you are comparing same indices of the same array,always true
    {
        flag=1;   //executed when i=0,j=0
        break;
    }
}

if(flag==1)
{
    printf(" palindrome");
}
else
{
    printf("else Palindrome");
}

So you are bound to get palindrome all the time. You can try this:

int m=n/2,flag=0;
for(int i=0;i<m;i++)
    if(s[i]==s[n-i-1])
        flag++;
if(flag==m)
    //palindrome;
else
    //not

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

I think the requirement of the assignment means that you may not use standard string functions.

To determine whether a string is a palindrome there is no need to create a copy of the string and to reverse the copy. You can check this "in place".

Here is a demonstrative program

#include <stdio.h>

int is_palindrome( const char *s )
{
    size_t n = 0;

    while ( s[n] != '\0' ) n++;

    size_t i = 0;

    while ( i < n / 2 && s[i] == s[n-i-1] ) i++;

    return i == n / 2;
}

#define  N  100

int main( void ) 
{
    char s[N];


    fgets( s, sizeof( s ), stdin );

    size_t n = 0;

    while ( s[n] != '\0' && s[n] != '\n' ) n++;

    if ( s[n] ) s[n] = '\0';

    if ( is_palindrome( s ) ) printf( "%s is a palindrome\n", s );

    return 0;
}

For example if to enter string ABCDEDCBA then the program output will be

ABCDEDCBA is a palindrome

As for your program then it uses the standard string function strlen. The copy of the original string is not zero terminated. And this loop

for(i=0,j=0;i<=n,j<=n;i++,j++)
{
//for(j=0;j<n;j++){
if(t[j]==t[i])
{

flag=1;
break;

 }

does not make sense even if you used correct indices.

Take into account that function gets is unsafe and is not supported by the C Standard any more. Instead use standard function fgets that appends inputted strings with the new line character (if there is enough space in the string) that you should to remove.

Also function main without parameters in C should be declared like

int main( void )

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

Not preventing using library functions, mistakes in your code are:

  • gets(), which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11, is used.
  • undefined behavior is invoked at printf("%s",t);, where pointer to what is not a null-terminated string is passed for use with %s.
  • The condition t[j]==t[i] is wrong because the same things are always compared.
  • You used flag to check if "at least any one of the characters is same". You should check "all of the characters are same".

Try this:

#include<stdio.h>
#include<string.h>
int main(void)
{
    char s[1000],*lf;
    char t[1000],temp;
    int i,j,n,flag=1;

    /* use fgets() instead of gets() */
    fgets(s, sizeof(s), stdin);
    if((lf=strchr(s, '\n'))!=NULL)
    {
        *lf = '\0';
    }

    n=strlen(s);
    for(i=0;i<n;i++)
    {
        t[i]=s[i];
    }
    /* terminate the string */
    t[n]='\0';
    printf("%s",t);

    n=strlen(s)-1;
    for(j=0,i=n;i>=0;j++,i--)
    {
        t[j]=s[i];
    }
    printf("%s",t);

    for(i=0;i<=n;i++)
    {
        /* check if all characters are same */
        if(s[i]!=t[i])
        {
            flag=0;
            break;
        }
    }

    if(flag==1)
    {
        printf(" palindrome");
    }
    else
    {
        printf("else Palindrome");
    }
    return 0;
}

Library functions are not avoided in this code. You can avoid using them by implementing what is (somewhat) equivalent to them by yourself and replacing usage of library functions to your implimentation.

Upvotes: 1

Related Questions