forked from mangwang/beginning_python_source_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting17-6.c
More file actions
37 lines (33 loc) · 973 Bytes
/
listing17-6.c
File metadata and controls
37 lines (33 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <Python.h>
static PyObject *is_palindrome(PyObject *self, PyObject *args) {
int i, n;
const char *text;
int result;
/* "s" means a single string: */
if (!PyArg_ParseTuple(args, "s", &text)) {
return NULL;
}
/* The old code, more or less: */
n=strlen(text);
result = 1;
for (i=0; i<=n/2; ++i) {
if (text[i] != text[n-i-1]) {
result = 0;
break;
}
}
/* "i" means a single integer: */
return Py_BuildValue("i", result);
}
/* A listing of our methods/functions: */
static PyMethodDef PalindromeMethods[] = {
/* name, function, argument type, docstring */
{"is_palindrome", is_palindrome, METH_VARARGS, "Detect palindromes"},
/* An end-of-listing sentinel: */
{NULL, NULL, 0, NULL}
};
/* An initialization function for the module (the name is
significant): */
PyMODINIT_FUNC initpalindrome() {
Py_InitModule("palindrome", PalindromeMethods);
}