2 Different C and C++ Programs to Check Whether a Given String is Palindrome or Not

Here i am writing two different programs to check whether a given string (text) us palindrome or not. There are C and C++ programs for this.

C Programs:

(i) Using string functions:




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

(ii) Comparing Character by Character


#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int len,i;
char a[100];
printf("Enter the string to check whether it is palindrome or not\n");
gets(a);
len=strlen(a);
for(i=0;i<len/2;i++)
{
if(a[i]!=a[len-i-1])
break;
}

if(i==len/2)
printf("\nEntered string is palindrome");
else
printf("\nEntered string is not palindrome");
getch();
}

C++ Programs:

(i) Using String functions:




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

(ii) Comparing Character by Character
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int len,i;
char a[100];
cout<<"Enter the string to check whether it is palindrome or not\n";
gets(a);
len=strlen(a);
for(i=0;i<len/2;i++)
{
if(a[i]!=a[len-i-1])
break;
}
if(i==len/2) cout<<"\nEntered string is palindrome"; else cout<<"\nEntered string is not palindrome"; getch(); }

No comments :

Post a Comment