The Program will accept a string of characters and report whether or not it’s a
ID: 675330 • Letter: T
Question
The Program will accept a string of characters and report whether or not it’s a palindrome and, if not, see if it is a palindrome that suffers corruption from a single errant character.
Examples :
Palindrome Decorruptionator
Enter string : abbac
abbac is not a palindrome.
abbac was corrupted! It is the palindrome abba
Enter string : Was itt a car or a cat I saw
Was itt a car or a cat I saw is not a palindrome.
Was itt a car or a cat I saw was corrupted! It is the palindrome wasitacarorcatisaw
Enter string : accurate
accurate is not a palindrome.
accurate is not a corrupted palindrome.
[Note: Multiple examples shown are for demonstration only, program should ask for one string, report the results, and return to the main menu]
Note that the program does not have to replace spacing, punctuation, and capitalization for the corrupted palindrome, we have other agents working on tools to provide that service.
Explanation / Answer
#include <iostream>
using namespace std;
#define NO_OF_CHARS 256
/* function to check whether characters of a string can form
a palindrome */
bool canFormPalindrome(char *str)
{
// Create a count array and initialize all values as 0
int count[NO_OF_CHARS] = {0};
// For each character in input strings, increment count in
// the corresponding count array
for (int i = 0; str[i]; i++)
count[str[i]]++;
// Count odd occurring characters
int odd = 0;
for (int i = 0; i < NO_OF_CHARS; i++)
if (count[i] & 1)
odd++;
// Return true if odd count is 0 or 1, otherwise false
return (odd <= 1);
}
/* Driver program to test to pront printDups*/
int main()
{
canFormPalindrome("geeksforgeeks")? cout << "Yes ":
cout << "No ";
canFormPalindrome("geeksogeeks")? cout << "Yes ":
cout << "No ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.