on Leave a Comment

C++ Program to Check String is Palindrome or not

C++ Program to Check Whether a String is Palindrome or not

TOPICS:

(1) C++ Program to Check Whether C-Style String is Palindrome or not

(2) C++ Program to Check String Object is Palindrome or not

C++ Program to Check Whether C-Style String is Palindrome or not

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char str1[50], str2[50];
    int i, j;
    cout<<"Enter a String: ";
    cin.getline(str1, 50);
    for(i=strlen(str1)-1, j=0; str1[j]!='\0'; i--, j++)
    {
        str2[j] = str1[i];
    }
    str2[j]= '\0';
    if(strcmp(str1, str2))
        cout<<"String is not Palindrome";
    else
        cout<<"String is Palindrome";
    return 0;
}

OUTPUT:

Enter a String: madam
String is Palindrome

C++ Program to Check String Object is Palindrome or not

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    string str1, str2;
    int i, j;
    cout<<"Enter a String: ";
    getline(cin, str1);
    str2 = str1;
    j = str1.length();
    j--;
    for(i=0; i<str1.length(); i++, j--)
    {
        str2[j] =str1[i];
    }
    if(str1 == str2)
        cout<<"\nString is Palindrome";
    else
        cout<<"\nString is not Palindrome";
    return 0;
}

OUTPUT:

Enter a String: mam

String is Palindrome

0 comments:

Post a Comment