Monday, 9 November 2015

C++ Program To Check String Is Palindrome Or Not

Problem :- Write A C++ Program To Check String Is Palindrome Or Not .

Logic :- What Is Palindrome ?
"a word, phrase, or sequence that reads the same backwards as forwards"

" Matlab Ulta Seedha Ek Saman "

Simple if string is "MADAM" compare First 'M' to Last 'M' same continue repeat if all Word's are shame then String Is Palindrome

Solution :-

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

int main()
{
  //By-Ghanendra Yadav
    char string1[20];
    int i, length;
    int flag = 0;
 
    cout << "Enter a string: ";
    cin >> string1;
 
    length = strlen(string1);
 
    for(i=0;i < length ;i++)
{
        if(string1[i] != string1[length-i-1])
{
            flag = 1;
            break;
    }
 }
 
    if (flag) 
{
        cout << string1 << " is not a palindrome" << endl;
    }  
    else {
        cout << string1 << " is a palindrome" << endl;
    }
    return 0;
}

Output:-

C++ Program To Check String Is Palindrome Or Not

No comments:
Write comments

Recommended Posts × +