Answers for "check if a string is palindrome c++"

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
2

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
0

palindrome c++

#include<iostream>
#include<string>
#include<algorithm>
bool IsPalindrome_true_false(const std::string& );
int main ()
{
    
    std::cout<<"Please enter a string:t";
    std::string str;
    getline(std::cin, str);
    
    // convert the string from uppercase to lowercase 
    int i = 0;
    while(str[i])
    {
        if(str[i] == std::toupper(str[i]) && std::isalpha(str[i]) == 1024)
        str[i]+= 32;
        ++i;
    }
    // looping while string is empty 
    while(str.empty())
    {
        std::cout<<"nPlease enter a string your string is empty:t";
        if(!str.empty())
        std::string str;
        getline(std::cin, str);
    }
    
    std::cout<<"n"<<std::boolalpha<<IsPalindrome_true_false(str)<<std::endl;
    std::cout<<std::endl;

    return 0;
}

// check if string is a palindrome and return true or false 
bool IsPalindrome_true_false(const std::string& str)
{

    int i = 0;                
    int j = str.length() - 1; 

    while(i <= j )
    {   
        
        if(std::isalpha(str[i]) == 0){
            ++i;
            continue;
        }else if(std::isalpha(str[j]) == 0){
            --j;
            continue;
        }   
        if(str[i] != str[j]){
        
           return false;
        }
        ++i;
        --j;
    }  
    return true;
}
Posted by: Guest on January-02-2021
-1

check if palindrome

function isPalindrome(str) {
  str = str.toLowerCase();
  return str === str.split("").reverse().join("");
}
Posted by: Guest on August-17-2020

Code answers related to "check if a string is palindrome c++"

Browse Popular Code Answers by Language