C++ Instructions Write a program that uses the function isPalindrome given in Ex
ID: 3755477 • Letter: C
Question
C++
Instructions
Write a program that uses the function isPalindrome given in Example 6-6 (Palindrome). Test your program on the following strings:
madam, abba, 22, 67876, 444244, trymeuemyrt
Modify the function isPalindrome of Example 6-6 so that when determining whether a string is a palindrome, cases are ignored, that is, uppercase and lowercase letters are considered the same.
The isPalindrome function from Example 6-6has been included below for your convenience.
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;
} // if
} // for loop
return true;
}// isPalindrome
Explanation / Answer
Use a utility function toupper() which returns upper case letters ascii value
Since we are converting each character to uppercase so case sensitivity does not matter now
CODE
#include <iostream>
#include <string>
using namespace std;
bool isPalindromeCaseInsensitive(string str)
{
int length = str.length();
for (int i = 0; i < length / 2; i++) {
if (toupper(str[i]) != toupper(str[length - 1 - i])) {
return false;
}
} // for loop
return true;
}
int main()
{
string test1 = "madam";
string test2 = "abbA";
string test3 = "67876";
string test4 = "444244";
string test5 = "trymeuemyrt";
cout << isPalindromeCaseInsensitive(test1) << endl;
cout << isPalindromeCaseInsensitive(test2) << endl;
cout << isPalindromeCaseInsensitive(test3) << endl;
cout << isPalindromeCaseInsensitive(test4) << endl;
cout << isPalindromeCaseInsensitive(test5) << endl;
}
ONly 444244 not palindrome
Please comment for any assistance
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.