Answers for "to check string is palindrome or not"

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
3

check a string is palindrome or not

#include <stdio.h>
#include <string.h>
int main()
{

     char str[80];
     int length;
     printf("nEnter a string to check if it's a palindrome: ");
     scanf("%s", str);     // string input
     length = strlen(str); // finding the string length
     int i, j, count;
     for (i = 0, j = length - 1, count = 0; i < length; i++, j--)
     {
          // matching string first character with the string last character
          if (str[i] == str[j])
          {
               count++; // if character match, increasing count by one.
          }
     }
     if (count == length) // if count == length, then the string is a palindrome.
     {
          printf("'%s' is a palindrome.n", str);
     }
     else // otherwise the string is not a palindrome.
     {
          printf("'%s' is not a palindrome.n", str);
     }
     return 0;
}
Posted by: Guest on July-04-2021
-1

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

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

Code answers related to "to check string is palindrome or not"

Browse Popular Code Answers by Language