+ 2

How to check string is palindrome or not using array and for loop in c++ simple and easy way?

5th Jan 2018, 11:31 AM
Rafael Rana
Rafael Rana - avatar
7 Answers
5th Jan 2018, 1:06 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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(); }
5th Jan 2018, 12:50 PM
Lokendra Soni
Lokendra Soni - avatar
+ 2
why is conio header file used ???
5th Jan 2018, 3:33 PM
Shariff
Shariff - avatar
+ 2
oh.....thankuuuuu @soni😀
6th Jan 2018, 3:45 AM
Shariff
Shariff - avatar
+ 1
reverse the string then check it with original string
5th Jan 2018, 1:07 PM
shobhit
shobhit - avatar
+ 1
@Noor Nawaz Shariff conio header is used for using CONSOLE INPUT AND OUTPUT functions like clrscr(); getch() and many more
5th Jan 2018, 3:36 PM
Lokendra Soni
Lokendra Soni - avatar
0
thanx @soni
11th Jan 2018, 7:44 PM
Rafael Rana
Rafael Rana - avatar