palindrome program check string in c++ code example
Example 1: check if a string is palindrome cpp
#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;
}
Example 2: 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);
int i = 0;
while(str[i])
{
if(str[i] == std::toupper(str[i]) && std::isalpha(str[i]) == 1024)
str[i]+= 32;
++i;
}
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;
}
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;
}