listing17-6.c

来自「《Beginning Python--From Novice to Profes」· C语言 代码 · 共 37 行

C
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);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?