Skip to main content

C++ program to check whether a string is palindrome or not


Write a function that checks whether a string is Palindrome or not. Use this function to check user-entered strings. Also count and display the number of odd or even palindromes in it

#include<iostream>
#include<string>
using namespace std;

static int count_even, count_odd;

void palindrome(string &a)
{
char tmp;
bool chk=false;
int slen=a.length();
string str2=a;

for(int i=0;i<slen;i++)
{
if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z'))
chk=true;
else
chk=false;
}

if(chk)
{
// Code to reverse string
for(int n=0;n<slen/2;n++)
{
tmp=a[n];
a[n]=a[slen-1-n];
a[slen-1-n]=tmp;
}
}
else
{
cout<<"\n User entered a non-valid string\n Please enter a new string";
return;
}

if(str2==a)
{
if(slen%2==0)
count_even++; // length of the string is even
else
count_odd++; // length of the string is odd

cout<<"\n Entered string is a palindrome";
}
else
cout<<"\n Enter string is not a palindrome";
}

void count()
{
cout<<"\n\n No. of palindromes (of even length) = "<<count_even<<"\n";
cout<<"\n\n No. of palindromes (of odd length) = "<<count_odd<<"\n";
}

Void main()
{
string str;
char ch;
do
{
system("cls");
cout<<"Enter a string : ";
cin>>str;

palindrome(str);

cout<<"\n\n\n Wanna enter more strings...press (y/Y)\n Press any to Exit\n";
cin>>ch;

}while(ch=='y' || ch=='Y');

count();

system("pause");
}

Comments

  1. Ahaa, its niсe conveгѕatiоn on the topіс
    of this post heге at this blog,
    I have гead all that, so now me also cоmmenting
    here.
    My web site samsung galaxy s3

    ReplyDelete
  2. Mу spοuse and I stumbled over heгe bу
    a diffeгent wеbѕite anԁ thought I shοuld
    check things out. I like what I see ѕo now i am following you.
    Look forward to looking at your web ρage гepeatedly.
    My web-site - trading strategies forex

    ReplyDelete
  3. Ιt's amazing in support of me to have a website, which is good designed for my knowledge. thanks admin
    Take a look at my blog :: pikavippi

    ReplyDelete

Post a Comment

Popular posts from this blog

Use Case Diagram for Online Book Store

Linear search & Binary search using Template

Write a program to search an element from a list. Give user the option to perform Linear or Binary search. Use Template functions. #include<iostream> using namespace std; template <class T> void Lsearch(T *a, T item, int n) { int z=0; for(int i=0;i<n;i++) { if(a[i]== item) { z=1; cout<<"\n Item found at position = "<<i+1<<"\n\n"; } else if(z!=1) { z=0; } } if(z==0) cout<<"\n Item not found in the list\n\n"; } template <class T> void Bsearch(T *a, T item, int n) { int beg=0,end=n-1; int mid=beg+end/2; while((a[mid]!=item) && (n>0)) { if(item>a[mid]) beg=mid; else end=mid; mid=(beg+end)/2; n--; } if(a[mid]==item) cout<<"\n Item found at position = "<<mid+1<<"\n\n"; else cout<<"\n Item not found in the list\n\n"; } void main() { int iarr[10] = {2,42,56,86,87,99,323,546,767,886};

Online Movie Ticket Booking Sequence Diagram