401 - Palindromes

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

SR7
New poster
Posts: 4
Joined: Fri Aug 14, 2015 6:36 pm

Re: 401 - Palindromes why RUNTIME Error!!!

Post by SR7 » Fri Aug 14, 2015 6:57 pm

plz help!!

accepted :lol:

HuyTranQ
New poster
Posts: 2
Joined: Thu Sep 24, 2015 12:04 pm

Re: 401 - Palindromes

Post by HuyTranQ » Sun Sep 27, 2015 4:47 am

I passed all the tests in uDebug and this thread but still got WA. Can you please check my code?

Code: Select all

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;

//				"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
string mirror = " 1SE Z  8 A   3  HIL JMNO   2TUVWXY5";
string result[4] = {
	" -- is not a palindrome.",
	" -- is a regular palindrome." ,
	" -- is a mirrored string." ,
	" -- is a mirrored palindrome."
};
string line;

int isPalindrome(string const & text);
int isMirrored(string const & text);
char mirrorOf(char input);

int main()
{
	while (true)
	{
		getline(cin , line);
		if (line.empty())
			break;
		cout << line << result[isPalindrome(line) + isMirrored(line)] << endl << endl;
	}
	return 0;
}

int isPalindrome(string const & text)
{
	int length = text.length();
	int midLength = (length / 2);
	--length;
	for (int i = 0; i < midLength; ++i)
		if (text[i] != text[length - i])
			return 0;
	return 1;
}

int isMirrored(string const & text)
{
	int length = text.length();
	int midLength = (length / 2) + (length % 2);
	--length;
	for (int i = 0; i < midLength; ++i)
		if (text[i] >= 'a' ||
			text[i] != mirrorOf(text[length - i]))
			return 0;
	return 2;
}

char mirrorOf(char input)
{
	if (input >= 'A')
		return mirror[10 + input - 'A'];
	else
		return mirror[input - '0'];
}

Post Reply

Return to “Volume 4 (400-499)”