+ 2
How to check string is palindrome or not using array and for loop in c++ simple and easy way?
7 Answers
+ 2
Well, you don't need a loop for checking whether a string is a palindrome string or not. You can simply do it with string functions.
But, if you want the program with loop the I am posting both the methods(with and without loop).
Method-1: Without loop.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
int i;
clrscr();
cout<<"Enter a string: ";
cin>>a;
strcpy(b,a);
strrev(a);
strlwr(a);
strlwr(b);
i=strcmp(a,b);
if(i==0)
cout<<"String is palindrome";
else
cout<<"String is not palindrome";
getch();
}
-------------------------------------------------------------------------------------------
Method-2: With for loop.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
int i,n;
clrscr();
cout<<"Enter a string: ";
cin>>a;
strlwr(a);
n=strlen(a);
for(i=n-1;i>=0;i--)
b[(n-1)-i]=a[i];
for(i=0;i<n;i++)
{
if(a[i]==b[i])
continue;
else
break;
}
if(i!=n)
cout<<"String is not Palindrome";
else
cout<<"String is Palindrome";
getch();
}
+ 2
why is conio header file used ???
+ 2
oh.....thankuuuuu @soniđ
+ 1
reverse the string then check it with original string
+ 1
@Noor Nawaz Shariff
conio header is used for using CONSOLE INPUT AND OUTPUT functions like clrscr(); getch() and many more
0
thanx @soni