سؤال

I must do a program that tells me if a string is palindrome or not using the library string.h . I wrote the following code but the output is always "palindrome"

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

int main()
{
 char a[100],b[100]; 
 int i,k;  
 printf("Type the string \n");
 gets(a);
 k=strlen(a);
 for(i=0;i<strlen(a);i++)
 {
  a[i]=b[k]; 
  k--;                       
 }           //at the end of this code the string "b" should be the reverse of "a"
 k=strcmp(a,b);
 if (k!=0)   //here I check if a=b or not
 {printf("palindrome");}
 else
 {printf("not palindrome");}
 getch();
 return 0;
}

Example: When my input is "non" the output should be "palindrome", if the input is "ship" the output should be "not palindrome". Could anyone help me to find what is wrong?

هل كانت مفيدة؟

المحلول

I think it's the line

a[i]=b[k];

Doesn't this put the contents of b[k] (which you have not initialized) into a[i] (which you have populated with the get)? This overwrites the test value in a with blanks, (or whatever was in b's memory) Shouldn't you do the opposite?

But better is not to do it at all - you can just compare the characters in place in the a array.

k=strlen(a);
for(i=0; i<k/2; i++)
   if(a[i] != a[k-i]) 
      return "Not Palindrome";
return "Palindrome";                    

نصائح أخرى

   /**
    ** Name: palindrome.c
    ** Description: This program checks if a word is palindrome or not
    ** Aldo Núñez Tovar
    **/

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

    int
    isPalindrome ( char* str )
    {
        char* str2 = str + strlen ( str) - 1;

        while ( str < str2 )
        {
            if ( *str++ != *str2-- )
            {
                return 0;
            }
        }
        return 1;
    }

    int
    main ( void )
    {
        char* str = "racecar"; /* level, civic, rotor, racecar */

        printf ( "Is  %s  palindrome? %s\n", str, isPalindrome ( str )? "Yes": "No" );
        return 0;
    }

I fixed it for you, please note the comments:

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

int main()
{
     char a[100],b[100]; 
     int i;
     int stringLen;  
     printf("Type the string \n");
     gets(a);
     stringLen = strlen(a);
     for(i=0; i < stringLen; i++)
     {
         //First you want to copy to B not A...
         //second, you need to remove "1" from the size cause array start from "0".
         b[stringLen-1-i] = a[i];            
     }//at the end of this code the string "b" should be the reverse of "a"

     if (strcmp(a,b) == 0)   //0 mean equal !
     {
         printf("palindrome");
     }
     else
     {
         printf("not palindrome");
     }
     getch();
     return 0;
}

strcmp() returns zero value when both strings are equal. It must be something like this:

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

int main()
{
    char a[100],b[100]; 
    int i,k;  
    printf("Type the string \n");
    gets(a);
    k=strlen(a)-1;
    for(i=0;i<strlen(a);i++)
    {
        b[i]=a[k]; //assign to b not to a
        k--;                       
    }
    b[strlen(a)]='\0';//terminate the string with null character
    //at the end of this code the string "b" should be the reverse of "a"
    k=strcmp(a,b);
    if (k==0)   //here I check if a=b or not
    {printf("palindrome");}
    else
    {printf("not palindrome");}
    getch();
    return 0;
}

Your code says this:

k=strlen(a);

Fix this like

k=strlen(a)-1;

This is because if the length is 15, then array index 0 to 14 equals 15. So, reverse it from 14. That means length-1.

Got it?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top