Skip to main content

Palindrome String & Palindrome Number



 //Palindrome Number

                    A number that is equal to the reverse of that same number is called a palindrome number. For example, 3553, 12321, etc.


CODE

👇

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n,r,sum=0, temp;
  5. printf("Enter the number:");
  6. scanf("%d", &n);
  7. temp=n;
  8. while(n>0)
  9. {
  10. r=n%10;
  11. sum=(sum*10)+r;
  12. n=n/10;
  13. }
  14. if(temp==sum)
  15. printf(">>Palindrome Number<<\n");
  16. else
  17. printf(">>Not Palindrome<<\n");
  18. return 0;
  19. }

👉Execute👈

/*

Output

1]

Enter the number:5116532

>>Not Palindrome<<

2]

Enter the number:1551
>>Palindrome Number<<

*/



//Palindrome String

            Palindrome string if the reverse of that string is the same as the original string. For example, radar , level , etc. 


Palindrome String(Main File)


CODE

👇

  1. #define false 0
  2. #define true 1
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "palindrome.h"

  7. char str[20];

  8. void main()
  9. {
  10.       int i,l;
  11.      STACK s;
  12.      char str[20], ch;
  13.      initstack();
  14.   
  15.      printf("Enter a string:");
  16.      scanf("%s",str);
  17.      l = strlen(str);
  18.      printf("Length of string:%d",l);
  19.   
  20.      for(i=0; i<l; i++)
  21.     {
  22.           printf("\n push str=%c",str[i]);
  23.           push(str[i]);
  24.     }
  25.   
  26.     for(i=0; i<l; i++)
  27.    {
  28.        ch=pop();
  29.        printf("\n pop ch=%c",ch);
  30.        printf("\n str[%d]=%c",i,str[i]);
  31.     
  32.        if(ch!=str[i])
  33.       {
  34.            printf("\n>> '%s' IS NOT PALINDROME<<\n",str);
  35.            exit(0);
  36.       }
  37.    }
  38.    printf("\n>> '%s' IS PALINDROME<<\n",str);
  39. }

FILE


Palindrome String(Header File)


CODE

👇

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 10
  4. typedef struct
  5. {
  6.     char data[MAX];
  7.     int top;
  8. }STACK;

  9. STACK s;
  10. char ch;

  11. void initstack()
  12. {
  13.      s.top = -1;
  14. }

  15. int isfull()
  16. {
  17.       if(s.top == MAX-1)
  18.       return 1;
  19.       else
  20.       return 0;
  21. }

  22. int isempty()
  23. {
  24.       if(s.top == -1)
  25.       return 1;
  26.      else
  27.      return 0;  
  28. }

  29. void push(char ch)
  30. {
  31.       if(isfull())
  32.       printf(">>STACK IS FULL<<");
  33.       else
  34.      {
  35.           printf("\tchar=%c",ch);
  36.           s.top++;
  37.           s.data[s.top]=ch;
  38.           printf("\ttop=%d",s.top);
  39.      }
  40. }

  41. char pop()
  42. {
  43.      if(isempty())
  44.      printf(">>STACK IS EMPTY<<");
  45.      else
  46.     {
  47.          printf("\n\n pop top=%d",s.top);
  48.          ch=s.data[s.top];
  49.          s.top = s.top -1;
  50.          return ch;
  51.      }
  52. }

Output

/*

1]

Enter a string:madam

Length of string:5

 push str=m     char=m  top=0

 push str=a     char=a  top=1

 push str=d     char=d  top=2

 push str=a     char=a  top=3

 push str=m     char=m  top=4


 pop top=4

 pop ch=m

 str[0]=m


 pop top=3

 pop ch=a

 str[1]=a


 pop top=2

 pop ch=d

 str[2]=d


 pop top=1

 pop ch=a

 str[3]=a


 pop top=0

 pop ch=m

 str[4]=m

>> 'madam' IS PALINDROME<<



2]
Enter a string:computer
Length of string:8
 push str=c     char=c  top=0
 push str=o     char=o  top=1
 push str=m     char=m  top=2
 push str=p     char=p  top=3
 push str=u     char=u  top=4
 push str=t     char=t  top=5
 push str=e     char=e  top=6
 push str=r     char=r  top=7

 pop top=7
 pop ch=r
 str[0]=c
>> 'computer' IS NOT PALINDROME<<

*/




                                                                         //ThE ProFessoR

Comments