Pages

Monday, 24 August 2015

C++ Program to check whether a number is palindrome or not (through pointers)


#include <iostream>
using namespace std;

class palinDrome
{
private:

char *myInput;
char *inverted;
int size;


public:
palinDrome()
{
myInput = NULL;
inverted = NULL;
size = 0;
}


void input();
void check();
};



//------------------------------------------Function to take Input----------------------------------------------//
void palinDrome :: input()
{

cout << endl;
cout << " Size of word: ";
cin >> size;
myInput = new char[size];
inverted = new char[size];

cout << endl;
cout << endl;
cout << " Input: ";
for (int i=0; i < size; i++)
{
cin >> myInput[i];
}

}

//----------------------------------------Function to check Is it Palindrome or Not ---------------------------------------//

void palinDrome:: check()
{

int j=0;

for (int i=size-1; i >= 0; i--)
{

inverted[j] = myInput[i];
j++;
}

cout << " Inverted: " ;             // Stroring one array into another array in Reverse order
for (int i=0; i < size; i++)
{
cout << inverted[i] ;
}

cout << endl;
cout << endl;

for (int i=0; i < size; ) // Condition to check Palindrome
{

if (myInput[i] == inverted[i])
{

if (i == size -1 ) // If Palindrome
{
system("cls");
cout << endl;
cout << "                            It is a Palindrome" << endl;
cout << endl;
cout << " Orignal Input : ";

for (int i=0; i < size; i++)
{
cout << myInput[i] ;
}
cout << endl;
cout << " Inverted Input: " ;
for (int i=0; i < size; i++)
{
cout << inverted[i] ;
}
cout << endl;
cout << "    _________________________________________________________________________" << endl;
cout << "                              Hence Proved"<< endl;
cout << "    _________________________________________________________________________" << endl;
cout << endl;
system("pause");
break;

}

i++;
}

else              // If not a palindrome
{
system("cls");
cout << endl;
cout << "                            It is NOT a Palindrome" << endl;
cout << endl;
cout << " Orignal Input : ";

for (int i=0; i < size; i++)
{
cout << myInput[i] << endl;
}
cout << " Inverted Input: " ;
for (int i=0; i < size; i++)
{
cout << inverted[i] << endl;
}
cout << endl;
cout << "    _________________________________________________________________________" << endl;
cout << "                              Hence Proved"<< endl;
cout << "    _________________________________________________________________________" << endl;
cout << endl;
system("pause");
break;
}


}



}


int main ()
{
int choose;
palinDrome p;
A:
system("cls");
cout << endl;
cout << endl;
cout << " <1> Input" << endl;
cout << endl;
cout << " <2> Check" << endl;
cout << endl;


cout << " Choose: ";
cin >> choose;

if (choose == 1)
{
system("cls");
p.input();
goto A;
}

if (choose == 2)
{
system("cls");
p.check();
goto A;
}

}

For any query email us : mohsin.mahmood@hotmail.co.uk

2 comments:

Ads.txt said...

thankyou so much . Its really awesome and helpful. I appreciate you

Unknown said...

Thankyou. Keep visiting our blog for more interesting posts.