Answers for "to check if string is palindrome"

C++
1

check if a string is palindrome cpp

// Check whether the string is a palindrome or not.
#include <bits/stdc++.h>

using namespace std;

int main(){
    string s;
    cin >> s;
    
    int l = 0;
    int h = s.length()-1;

    while(h > l){
        if(s[l++] != s[h--]){
            cout << "Not a palindrome" << endl;
            return 0;
        }
    }
    cout << "Is a palindrome" << endl;
    return 0;

}
Posted by: Guest on January-23-2021
0

reads the string in then determines if the string is a palindrome.

#include <iostream>
using namespace std;
 
// Iterative function to check if given string is a palindrome or not
bool isPalindrome(string str)
{
    int low = 0;
    int high = str.length() - 1;
 
    while (low < high)
    {
        // if mismatch happens
        if (str[low] != str[high])
            return false;
 
        low++;
        high--;
    }
 
    return true;
}
 
int main()
{
    string str = "XYXYX";
 
    if (isPalindrome(str))
        cout << "Palindrome";
    else
        cout << "Not Palindrome";
 
    return 0;
}
Posted by: Guest on June-25-2020

Code answers related to "to check if string is palindrome"

Browse Popular Code Answers by Language