Here are C++ programs to check whether a string is palindrome or not using recursion,using functions, using pointers, without using inbuilt functions etc.
Method 1 : C++ program to check whether a string is palindrome or not
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char a[10],rev[10],*ft,*lt;
cout<<"Enter the string ";
gets(a);
strcpy(rev,a);
strrev(rev);
if(strcmp(a,rev)==0)
cout<<"String is palindrome ";
else
cout<<"String is not palindrome";
return 0;
}
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char a[10],rev[10],*ft,*lt;
cout<<"Enter the string ";
gets(a);
strcpy(rev,a);
strrev(rev);
if(strcmp(a,rev)==0)
cout<<"String is palindrome ";
else
cout<<"String is not palindrome";
return 0;
}
Method 2 : C++ program to check whether a string is palindrome or not using pointers
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[10],*ft,*lt,flag=1;
int i,y;
cout<<"Enter the string ";
gets(a);
i=strlen(a)-1;
lt=&a[i];
ft=&a[0];
for(y=i;y>=i/2;y--,ft++,lt--)
if(*lt!=*ft)
flag=0;
if(flag==1)
cout<<"String is palindrome ";
else
cout<<"String is not palindrome ";
return 0;
}
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[10],*ft,*lt,flag=1;
int i,y;
cout<<"Enter the string ";
gets(a);
i=strlen(a)-1;
lt=&a[i];
ft=&a[0];
for(y=i;y>=i/2;y--,ft++,lt--)
if(*lt!=*ft)
flag=0;
if(flag==1)
cout<<"String is palindrome ";
else
cout<<"String is not palindrome ";
return 0;
}
Method 3 : C++ program to check whether a string is palindrome or not using functions
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int compare(char a[10])
{
char rev[10];
strcpy(rev,a);
strrev(rev);
if(strcmp(a,rev)==0)
return 1;
else
return 0;
}
int main()
{
char a[10],*ft,*lt;
cout<<"Enter the string ";
gets(a);
if(compare(a))
cout<<"String is palindrome ";
else
cout<<"String is not palindrome ";
return 0;
}
#include<stdio.h>
#include<string.h>
using namespace std;
int compare(char a[10])
{
char rev[10];
strcpy(rev,a);
strrev(rev);
if(strcmp(a,rev)==0)
return 1;
else
return 0;
}
int main()
{
char a[10],*ft,*lt;
cout<<"Enter the string ";
gets(a);
if(compare(a))
cout<<"String is palindrome ";
else
cout<<"String is not palindrome ";
return 0;
}
Method 4 : C++ program to check whether a string is palindrome or not using without using any in-built functions
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char a[10],lt;
cout<<"Enter the string ";
gets(a);
lt=strlen(a)-1;
for(int i=0;i<=lt/2;i++)
if(a[lt-i]!=a[i])
{
cout<<"String is not palindrome ";
return 0;
}
cout<<"String is palindrome ";
return 0;
}
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char a[10],lt;
cout<<"Enter the string ";
gets(a);
lt=strlen(a)-1;
for(int i=0;i<=lt/2;i++)
if(a[lt-i]!=a[i])
{
cout<<"String is not palindrome ";
return 0;
}
cout<<"String is palindrome ";
return 0;
}
Method 5 : C++ program to check whether a string is palindrome or not using recursion
#include<iostream>
#include<string.h>
using namespace std;
void cpalin(char *str,int i,int j)
{
if(i>j)
{
cout<<"String is palindrome";
return ;
}
else if(str[i]!=str[j])
{
cout<<"String is not palindrome";
return ;
}
cpalin(str,i+1,j-1);
}
int main()
{
char str[50];
int lenght;
cout<<"Enter string : ";
cin>>str;
cpalin(str,0,strlen(str)-1);
return 0;
}
#include<string.h>
using namespace std;
void cpalin(char *str,int i,int j)
{
if(i>j)
{
cout<<"String is palindrome";
return ;
}
else if(str[i]!=str[j])
{
cout<<"String is not palindrome";
return ;
}
cpalin(str,i+1,j-1);
}
int main()
{
char str[50];
int lenght;
cout<<"Enter string : ";
cin>>str;
cpalin(str,0,strlen(str)-1);
return 0;
}
No comments:
Post a Comment