Showing posts with label palindrome. Show all posts
Showing posts with label palindrome. Show all posts

Tuesday, November 22, 2011

C Program to Check String is Palindrome or Not

It is a sample C language example. It is a string palindrome program which accept a string from user.
It checks that accepted string is a palindrome string or not. If string or string reverse is as same as then this string is a palindrome string. It means read string same in both forward or backward direction.



Aim - C program to check whether the accepted string is palindrome or not.


Program Source Code
//C Program to check accepted string is palindrome or not
#include"stdio.h"
#include"conio.h"
int isPallindrome(char *);
void main()
{
  int a;
  char str1[30];
  clrscr();
  printf("\n******String Palindrome******\n");
  printf("Enter a string: ");
  gets(str1); // accept string from user
  a=isPallindrome(str1);  /*function calls, accept string as argument and return flag value*/

  if(a)
   printf("\nstring: %s is a palindrome string",str1);
  else
   printf("\nstring: %s is not a palindrome string",str1);
  getch();

}


int isPallindrome(char *str2)
{
   int i,j,flag=1;

   for(i=0,j=strlen(str2)-1; i<j; i++, j--)
 {
    if(str2[i]!=str2[j]) /*starts checking from ends of string to middle*/
  {
    flag=0; /*if reverse of string is not same than return 0*/
    break;
  }

 }
   return(flag);
}


Output1
******String Palindrome******
Enter a string: MADAM
string: MADAM is a palindrome string

Output2
******String Palindrome******
Enter a string: HELLO
string: HELLO is not a palindrome string




Thursday, November 11, 2010

C Program to check number is palindrome or not



C Program Code


//Check number is palindrome of not
#include "stdio.h"
#include "conio.h"
void main()
{
int num,rev=0,m,r;
clrscr();
printf("enter any number");
scanf("%d",&num);
m=num;
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
if(rev==m)
printf("given number is palindrome");
else
printf("given number is not palindrome");
getch();
}

Input/Output


enter any number 121
given number is palindrome

enter any number 423
given number is not palindrome

What is Palindrome No?


when we read a number. If backward read is as same as forward read then number is palindrome.