Answer:
a) # include <iostream>
# include <conio.h>
# include <stdio.h>
# include <string.h>
using namespace::std;
bool IsPalindrome(const string& str)
{
if (str.empty())
return false;
int i = 0; // first characters
int j = str.length() - 1; // last character
while (i < j)
{
if (str[i] != str[j])
{
return false;
}
i++;
j--;
}
return true;
}
int main()
{
string str;
cout<<"Enter the string";
getline(cin, str);
bool a= IsPalindrome(str);
if(a)
{
cout<<"Is in palindrome";
}
else
{
cout<<"Is not in palindrome";
}
}
b) Modified version of function with full program to discard uppercase or lowercase is as below:
# include <iostream>
# include <conio.h>
# include <stdio.h>
# include <string.h>
using namespace::std;
bool IsPalindrome(const string& str)
{
if (str.empty())
return false;
int i = 0; // first characters
int j = str.length() - 1; // last character
while (i < j)
{
char a= tolower(str[i]);
char b= tolower(str[j]);
if (a != b)
{
return false;
}
i++;
j--;
}
return true;
}
int main()
{
string str;
cout<<"Enter the string";
getline(cin, str);
bool a= IsPalindrome(str);
if(a)
{
cout<<"Is in palindrome";
}
else
{
cout<<"Is not in palindrome";
}
}
Explanation:
Please check the answer section.