Search This Blog

Friday, September 23, 2011

write program for palindrome

FOR NUMBERS:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,m;
clrscr();
printf("enter any no");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(m==n)
printf("the no is palindrome");
else
printf("no is not palindrome");
getch();
}



FOR STRINGS:

#include<stdio.h>
#include<conio.h>
#include<string.h>
 
main()
{
   char a[100], b[100];
 
   printf("Enter the string to check if it is a palindrome\n");
   gets(a);
 
   strcpy(b,a);
   strrev(b);
 
   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a pailndrome.\n");
 
   getch();
   return 0;
}


USING DATA STRUCTURES:

#include<stdio.h>
 
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
 
main()
{
      char string[100];
      int result;
 
      printf("Enter a string\n");
      gets(string);
 
      result = is_palindrome(string);
 
      if ( result == 1 )
         printf("\"%s\" is a palindrome string.\n", string);
      else
         printf("\"%s\" is not a palindrome string.\n", string); 
 
      return 0;
}
 
int is_palindrome(char *string)
{
      int check, length;
      char *reverse;
 
      length = string_length(string);    
      reverse = (char*)malloc(length+1);    
 
      copy_string(reverse, string);
      reverse_string(reverse);
 
      check = compare_string(string, reverse);
 
      free(reverse);
 
      if ( check == 0 )
         return 1;
      else
         return 0;
}
 
int string_length(char *string)
{
   int length = 0;  
 
   while(*string)
   {
      length++;
      string++;
   }
 
   return length;
}
 
void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}
 
void reverse_string(char *string) 
{
     int length, c;
     char *begin, *end, temp;
 
     length = string_length(string);
 
     begin = string;
     end = string;
 
     for ( c = 0 ; c < ( length - 1 ) ; c++ )
         end++;
 
 
     for ( c = 0 ; c < length/2 ; c++ ) 
     {        
         temp = *end;
         *end = *begin;
         *begin = temp;
 
         begin++;
         end--;
     }
}
 
int compare_string(char *first, char *second)
{
   while(*first)
   {
      if (*first == *second)
      {
         first++;
         second++;
      }     
      else
         return -1;
   }
 
   return 0;  
}


USING C++:

#include<iostream.h>
#include<string.h>

int main()
{
     char str[25],str1[25];	
     cout<<"Enter a string: ";
	
     gets(str);

     strcpy(str1,str);
     strrev(str);
	if(strcmp(str,str1)!=0)
	{
	     cout<<"string is not palindrome";
	}
	else
	{
	      cout<<"string is a palindrome";
	}

	  cout<<endl;


USING JAVA:

String s1=args[0];
String s2="";

for(int i=0; i<s1.length(); i++)
{
   s2=s2+charAt(i);
}
if(s1.equals(s2))
  System.out.println("Palindrome");
else
  System.out.println("Not Palindrome");
 

No comments:

Post a Comment