palindrome.....

Pages: 12
Q. Write a function to check weather a string is palindrome or not. The function will return 'y' if the passed string is a palindrome otherwise 'n' will be returned. The prototype of the function is given below:
char palindrome(char string[]);
Write a function to declare and obtain a string from the user and execute the above declared function.
1
2
3
4
5
bool palindrome(char string[]){
if (string == "pop")
return true;
return false;
}
1
2
3
4
5
#include <cstdio>
#include <cstring>
using namespace std;
int palindrone(char *str){unsigned i=0,e=1;do{(*(str+i)==*(str-1+strlen(str)-i))?(++i):(e=0);if(e==0)return 0;}while(i<strlen(str));return 1;}
int main(){if(palindrone("RACECAR")==1)printf("RACECAR IS RACECAR: %d",1);return 0;}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool palindrome(const char []); //wrapper
static bool palindrome(const char*, const char *, size_t);

bool palindrome(const char string[]){
	size_t size = strlen(string);
	if(size==1 or size==0) return true;
	return palindrome(string, string+size-size/2, size/2);
}
static bool palindrome(const char *p, const char *q, size_t size){
	if(size==1) return *p==*q;
	size_t midpoint = size/2;
	return 
		palindrome(p, q+size-midpoint, midpoint) and
		palindrome(p+midpoint, q, size-midpoint);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

bool _(const string& __,unsigned ___=0.f){return(___<int(__.size()/(('L'-'O'+'L')%2*2)))?(__[___]==__[__.length()-___-(0xFE>>'\a')])?_(__,___+(0xFF<<('F'-'U'-'C'+'K'))):___-___:!(0xFF>>010);}

int main()
{
    string message;
    getline(cin,message);
    cout << _(message);
    return 0;
}
Last edited on
@kyon

that returns false for non-palindromes and causes stack overflow for palindromes over 1 character long...I'm not even going to begin to try to find out why as that code just fries my brain O_o
The code works fine on my pc.
thats weird I copied it exactly, damn maverick computers
What compiler are you using?
Microsoft Visual C++
I'll try to compile the code on VC++ in a minute. It worked on G++ for me..
As I sat down on my desktop chair and opened up VC++, I remembered why I hated VC++. After a few attempts at making it do what I want, I gave up (project refused to let me add files to it). Instead, I just managed to compile the given code from above on the old-ass MingW compiler that bundles with Bloodshed Dev-Cpp.
Last edited on
Hey kyon, what is this little gem doing (pondering interrupt) ... I got everything up to that point

0xFE>>'\a'
It's just me rewriting constants:

0xFE is hexadecimal for 254.
>> is a right-bitshift.
'\a' is a character constant.

'\a' = 7
So the above would be 254>>7, which is a constant and equals 1.

Explanation of bitshifting in 254>>7:
1
2
3
4
5
6
7
8
9
10
254 = 111111102

11111110
87654321

Add 7 zeroes in front while the length remains 1 to 8 will get you this binary number:
000000011111110
87654321

Everything that goes beyond the 1 is thrown away. The result is converted back to decimal and is in this case 1.
Last edited on
1
2
0xFF<<('F'-'U'-'C'+'K');
0xFF<<-7; //warning: left shift count is negative 

"The behaviour [of a shift operation] is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand."
Last edited on
There is a warning, yet most compilers implement negative shifting as opposite shifting. So 0xFF<<-7 would converge to 0xFF>>7.
Maybe not in VC++
Good point.. Edited version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#define ABS(x) (x*(x>0 - x<0))
using namespace std;

bool _(const string& __,unsigned ___=0.f){return(___<int(__.size()/(('L'-'O'+'L')%2*2)))?(__[___]==__[__.length()-___-(0xFE>>'\a')])?_(__,___+(0xFF>>ABS('F'-'U'-'C'+'K'))):___-___:!(0xFF>>010);}

int main()
{
    string message;
    getline(cin,message);
    cout << _(message);
    return 0;
}
Last edited on
still gives me stack overflow

I get three warnings in the line containing the function

C4804: '<' : unsafe use of type 'bool' in operation
C4293: '>>' : shift count negative or too big, undefined behavior
C4800: 'unsigned int' : forcing value to bool 'true' or 'false' (performance warning)
Beautiful! Cryptic! Perfect! =)
Pages: 12