Friday, June 26, 2009

PROGRAM TO CHECK THE PALINDROME OF STRINGS

METHOD 1: Using String Handling Library Functions

#include "stdio.h"
#include "conio.h"
#include "string.h"

void main()
{
char str[25],temp[25];
clrscr();
printf("Enter String: ");
gets(str);
strcpy(temp,str);
strrev(str); /* Using Library Function to Reverse the String */

if(strcmp(temp,str)==0)
printf("\nPalindrome");
else
printf("\nNot Palindrome");
getch();
}

METHOD 2: Without Using Function strrev() (Better than Method 1)

#include
#include
#include
#include
#define TRUE 1
#define FALSE 0

void main()
{
char str[25];
int len,i,flag=TRUE;
clrscr();
printf("Enter String: ");
gets(str);
len=strlen(str);

for(i=0;i if(toupper(str[i])!=toupper(str[len-i-1]))/* comparing first++ and last-- chars */
flag=FALSE;
if(flag)
printf("\nPalindrome");
else
printf("\nNot Palindrome");
getch();
}

METHOD 3: Without Using any String Handling Functions.
(Different and Complex Logic than Method 2)

#include
#include
#include
#define TRUE 1
#define FALSE 0

void main()
{
char str[25];
int len,i,j,flag=TRUE;
clrscr();
printf("Enter String: ");

for(i=0;(str[i]=getchar())!='\n';i++); /* Scaning Characters Until User Hits Enter */
len=i-1;
for(i=0,j=len;i if(toupper(str[i])!=toupper(str[j])) /* comparing first++ & last--characters */
flag=FALSE;

if(flag)
printf("\nPalindrome");
else
printf("\nNot Palindrome");
getch();
}

0 comments:


by Ankit Pokhrel.