C LANGUAGE BOOK |
suppose u enter a string s1 say
eve
the reverse of eve s2 is
eve
hence s1 and s2 are equal
the string eve is palindrome.
suppose u enter a number n1 say
121
the reverse of number n2 is
121
hence n1 and n2 are equal
the number 121 is palindrome.
/*
WAP TO CHECK WHETHER A STRING ENTERED BY
THE USER.IS PALINDROME OR NOT.
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int xstrlen(char *);
char a[30],b[30];
int n,len,i;
clrscr(); //CLEARS THE OUTPUT SCREEN
/*Check String Palindrome*/
printf("Enter the String to Check for Palindrome: ");
gets(a); //TAKING THE STRING
len= strlen (a);
printf("\n the string is");
printf("\t%s" ,a); //PRINT THE ENTERED STRING
xstrcpy(a,b);//copy source string a into target string b
xstrrev(b);
printf("\t%s" ,b); //PRINT THE REVERSED STRING
i=xstrcmp(a,b);
if(i==0)
printf("String is a Palindrome ");
else
printf("String is not a Palindrome ");
/*Check Number Palindrome*/
printf("Enter the Number to Check for Palindrome: ");
scanf("%d",&n); //TAKING THE NUMBER
xnopal(n);
getch();
}
void xstrcpy(char *s,char *t)
{
while(*s!='\0')
{
*t=*s;
s++;
t++;
}
}
void xstrrev(char *s)
{ int length, c; char *begin, *end, temp; length = strlen(s); begin = s; end = s; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = 0 ; c < length/2 ; c++ ) { temp = *end; *end = *begin; *begin = temp; begin++; end--; } }
int xstrcmp(char *s,char *t)
{
int length;
length = strlen(s);
/*Comparing String*/
while(length>0)
{
if((*s)==(*t))
{
s++;
t++;
}
else
{ return -1; }
}
return 0;
}
void xnopal(int n)
{
int temp,remainder,rev=0;temp=n;
while(n>0)
{
remainder=temp%10; //Gets Last Digit
temp=temp/10; //Truncates Last Digit
rev=rev*0+remainder; //Builds reversed number
}
printf("\n%d" ,n); //PRINT THE ENTERED NUMBER
printf("\n%d" ,rev); //PRINT THE REVERSED NUMBER
if(rev==n)
printf ("\nThe Number Is A Palindrome ");
else
printf ("\nThe Number Is Not A Palindrome ");}
C LANGUAGE BOOK |
No comments:
Post a Comment