A string that is a palindrome reads the same forward and backward.
In CSNOBOL4 or SPITBOL, the program would look like:
s = trim(input)
t = \?(s ? reverse(s)) 'not '
output = s ' is ' t 'a palindrome'
end
In C style languages, it is a bit more difficult. You would strcmp the reverse of the string. To reverse a string, swap the first and last chars, then second-to-last, etc. until done. Or, do it recursively. The reverse of a string is the reverse of the string with the first char removed, with the char appended. Or, if you think palindromes are important, don't actually swap bytes, just compare, and bail on a mismatch.
Anyway, here is a palindrome written in C, using the swap reverse followed by string compare method:
/* */
#include
#include
char *reverse(char *s)
{
char *r = s, *p = s + strlen(s) - 1;
for (;s < p; --p, ++s) {
char c = *s;
*s = *p;
*p = c;
}
return r;
}
int main(void)
{
char string[80];
char *reversed;
gets(string);
reversed = reverse(strdup(string));
if (strcmp(string,reversed) == 0)
printf("palindrome\n");
else
printf("not palindrome\n");
free(reversed);
return 0;
}
2007-11-11 21:43:43
·
answer #1
·
answered by Fred W 5
·
0⤊
0⤋
position a pointer on the beggining of the string, and a pointer on the end of the string. Then evaluate both assistance contents. in the journey that they fluctuate it really is no longer a palindrome. Else, boost through 1 million the first pointer and shrink through 1 million the second one pointer, and repeat this till you attain the middle of the string. EDIT: social gathering: const char* pszLeft = myString; const char* pszRight = myString + strlen(myString) - a million; bool bPalindrome = real; at the same time as ( pszLeft < pszRight ) { if ( *pszLeft != *pszRight ) { bPalindrome = fake; smash; } ++pszLeft; --pszRight; }
2016-10-24 02:10:00
·
answer #2
·
answered by ecker 4
·
0⤊
0⤋
Isn't a palindrome such a text thing that reads same from end to front or front to end?
Then:
public class Answer {
public static void main(String[] line) {
String checkthis = line[0];
Sop(rev(checkthis) == checkthis);
}
public static String rev(String text) {
if (text.length() == 0) return "";
else return (rev(text.substring(1) + text.charAt(0));
}
}
2007-11-11 21:18:52
·
answer #3
·
answered by Andy T 7
·
0⤊
0⤋
a$ = "yahoohay" ' Input string
' ---------------------------------
b$ = REVERSE$(a$) ' Using language specific trick
IF a$ = b$ THEN PRINT "PALINDROME" ELSE PRINT "DIFFERENT"
' --- OR --------------------------
a$ = "yahoohay" ' Input string
b$ = ""
FOR i = 1 TO LEN(a$)
b$ =b$ + MID$(a$, LEN(a$)-i+1, 1) ' "Manually" reverse the string
NEXT
IF a$ = b$ THEN PRINT "PALINDROME" ELSE PRINT "DIFFERENT"
' --- OR --------------------------
a$ = "yahoohay" ' Input string
palindrome = 0 ' 0 MEANS PALINDROME, Assume
FOR i = 1 TO LEN(a$)
s1$ = MID$(a$, i, 1)
s2$ = MID$(a$, LEN(a$)-i+1, 1)
palindrome = palindrome + ABS(s1$ <> s2$) ' TRUE = -1
NEXT
IF palindrome = 0 THEN PRINT "PALINDROME" ELSE PRINT "DIFFERENT"
' ---------------------------------
2007-11-11 22:41:37
·
answer #4
·
answered by TBird 4
·
0⤊
0⤋