+92 332 4229 857 99ProjectIdeas@Gmail.com

Pallindrome or not (C++)


How to check whether a string is pallindrome or not?
isPallindrome is a function which takes a string as an input parameter and returns:
true in case if a given string is a pallindrome.
false in case if a given string is not a pallindrome.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

bool isPallindrome(char*);

int main()
{
       cout << isPallindrome ( "7861234321687" ) << endl;
       cout << isPallindrome ( "786687" ) << endl;
       cout << isPallindrome ( "1" ) << endl;
       cout << isPallindrome ( "123" ) << endl;
       cout << isPallindrome ( "321" ) << endl;
       cout << isPallindrome ( "11111111" ) << endl;

       _getche();
       return 0;
}

bool isPallindrome( char*str )
{
int lengthOfString = strlen( str ) ;

       for( int i = 0, n = lengthOfString - 1 ; i < lengthOfString/2 ; i++ , n-- )
       {
              if ( str[ i ] != str [ n ] )
                     return false;
       }
return true;
}

Output
1
1
1
0
0
1

0 comments: