133k views
0 votes
Write a program that uses the function isPalindrome given below. Test your program on the following strings: madam, abba, 22, 67876, 444244, trymeuemyrt. Modify the function isPalindrome given so that when determining whether a string is a palindrome, cases are ignored, that is, uppercase and lowercase letters are considered the same. bool isPalindrome(string str)

{
int length = str.length();

for (int i = 0; i < length / 2; i++)
{
if (str[i] != str[length - 1 - i] )
return false;
}

return true;
}

User The Walrus
by
7.4k points

1 Answer

1 vote

Answer:

#include <iostream>

#include <string>

using namespace std;

bool isPalindrome(string str)

{

int length = str.length();

for (int i = 0; i < length / 2; i++)

{

if (tolower(str[i]) != tolower(str[length - 1 - i]))

return false;

}

return true;

}

int main()

{

string s[6] = {"madam", "abba", "22", "67876", "444244", "trymeuemyrt"};

int i;

for(i=0; i<6; i++)

{

//Testing function

if(isPalindrome(s[i]))

{

cout << "\\ " << s[i] << " is a palindrome... \\";

}

else

{

cout << "\\ " << s[i] << " is not a palindrome... \\";

}

}

return 0;

}

User Srisar
by
6.8k points