⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listing17-6.c

📁 《Beginning Python--From Novice to Professional》 的源码
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -