CS140 Midterm Page 2


Question 5 - Your Programming Problem

A palindrome is a string that reads the same from left to right as it does from right to left. Write a procedure with the following prototype:

string s_to_p(string s);

Given a string s, s_to_p() should return a palindrome that is produced by changing the minimum possible number of characters in s. Changing a character means replacing it with any single character at the same position. You are not allowed to remove or add any characters. If there are multiple answers, return the one that comes first alphabetically.

Constraints:

Examples: I created an a.out by compiling s_to_p() with the following main:

#include <iostream>
using namespace std;

main()
{
  string s;

  while (cin >> s) cout << s_to_p(s) << endl;
}

Here are some examples:

UNIX> cat -n input.txt
     1	fred
     2	dontonio
     3	babydaisy
     4	abba
     5	abacab
     6	xxyy
     7	yyxx
     8	xxy
     9	madamimadam
UNIX> a.out < input.txt | cat -n
     1	deed
     2	dinoonid
     3	babadabab
     4	abba
     5	aaaaaa
     6	xxxx
     7	xxxx
     8	xxx
     9	madamimadam
UNIX>