计算机考试-C 回文计算—东方仙盟

Use VC++2010 to open the solution in the candidate folder blank1. The project contains one source file blank1.c. The function fun() judges whether the string pointed to by parameter s is a palindrome . If it is a palindrome, the function returns 1. If not, it returns 0. A palindrome reads exactly the same forward and backward, case‑insensitive. For example: "LEVEL" and "Level" are palindromes, while "LEVLEV" is not.

Complete the program by filling in the blanks. Do not add or delete lines, do not change program structure.

复制代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int fun(char * s)
{
    char * lp, * rp;
    /**********found**********/
    lp = 【1】 ;
    rp = s + strlen(s) - 1;

    while( (toupper(*lp)==toupper(*rp)) && (lp < rp) )
    {
        /**********found**********/
        lp ++; rp 【2】 ;
    }

    /**********found**********/
    if (lp < rp) 【3】 ;
    else return 1;
}

main()
{
    char s[81];
    printf("Enter a string: ");
    scanf("%s",s);
    if(fun(s))
        printf("\n\"%s\" is a Palindrome.\n\n",s);
    else
        printf("\n\"%s\" isn't a Palindrome.\n\n",s);
}

Answers

【1】s 【2】-- 【3】return 0


Explanation

Return value rule

  • return 1: It is a palindrome.
  • return 0: It is NOT a palindrome.

The program uses two pointers:

  • lp: left pointer, starts at the first character of string.
  • rp: right pointer, starts at the last character of string.

while( (toupper(*lp)==toupper(*rp)) && (lp < rp) ) Loop runs only when two conditions are both true:

  1. The uppercase of left‑side character equals uppercase of right‑side character.
  2. Left pointer is still before right pointer (lp < rp).

Inside loop: move left pointer forward, move right pointer backward toward the middle.

When the while loop stops:

  1. If lp < rp is still true → we met different characters. Not palindrome → return 0.
  2. If pointers meet or cross each other → all matched. Is palindrome → return 1.

Test case 1: Even‑length palindrome ABCCBA(偶数长度)

Characters: A B C C B A

  • lp points to A, rp points to A → equal, lp++, rp--
  • lp points to B, rp points to B → equal, lp++, rp--
  • lp points to C, rp points to C → equal, lp++, rp-- Now lp is at index 3, rp is at index 2. lp < rp3 < 2 → false. While loop exits. Skip if(lp<rp), execute else return 1. Result: it is a palindrome.

Test case 2: Odd‑length palindrome ABCBA(奇数长度)

Characters: A B C B A

  • lp=0(A), rp=4(A): equal → lp++, rp--
  • lp=1(B), rp=3(B): equal → lp++, rp-- Now lp and rp both point to middle character C(index 2). Enter while‑loop: characters equal, lp<rp (2<2) is false. Loop condition fails. Loop stops directly. Execute else return 1. Result: it is a palindrome.

One non‑palindrome example ABCDE

First compare A vs E: not equal. The while loop stops immediately. lp < rp holds true. Run return 0. Output: not a palindrome.

Why return 0? 0 stands for logical false in C language, meaning the proposition "this string is palindrome" is false. 1 stands for logical true, meaning the proposition holds true.

人人皆为创造者,共创方能共成长

每个人都是使用者,也是创造者;是数字世界的消费者,更是价值的生产者与分享者。在智能时代的浪潮里,单打独斗的发展模式早已落幕,唯有开放连接、创意共创、利益共享,才能让个体价值汇聚成生态合力,让技术与创意双向奔赴,实现平台与伙伴的快速成长、共赢致远。

原创永久分成,共赴星辰大海

原创创意共创、永久收益分成,是东方仙盟始终坚守的核心理念。我们坚信,每一份原创智慧都值得被尊重与回馈,以永久分成锚定共创初心,让创意者长期享有价值红利,携手万千伙伴向着科技星辰大海笃定前行,拥抱硅基 生命与数字智能交融的未来,共筑跨越时代的数字文明共同体。

东方仙盟:拥抱知识开源,共筑数字新生态

在全球化与数字化浪潮中,东方仙盟始终秉持开放协作、知识共享的理念,积极拥抱开源技术与开放标准。我们相信,唯有打破技术壁垒、汇聚全球智慧,才能真正推动行业的可持续发展。

开源赋能中小商户:通过将前端异常检测、跨系统数据互联等核心能力开源化,东方仙盟为全球中小商户提供了低成本、高可靠的技术解决方案,让更多商家能够平等享受数字转型的红利。

共建行业标准:我们积极参与国际技术社区,与全球开发者、合作伙伴共同制定开放协议 与技术规范,推动跨境零售、文旅、餐饮等多业态的系统互联互通,构建更加公平、高效的数字生态。

知识普惠,共促发展:通过开源社区 、技术文档与培训体系,东方仙盟致力于将前沿技术转化为可落地的行业实践,赋能全球合作伙伴,共同培育创新人才,推动数字经济 的普惠式增长

阿雪技术观

在科技发展浪潮中,我们不妨积极投身技术共享。不满足于做受益者,更要主动担当贡献者 。无论是分享代码、撰写技术博客,还是参与开源项目 维护改进,每一个微小举动都可能蕴含推动技术进步的巨大能量。东方仙盟是汇聚力量的天地,我们携手在此探索硅基 生命,为科技进步添砖加瓦。

Hey folks, in this wild tech - driven world, why not dive headfirst into the whole tech - sharing scene? Don't just be the one reaping all the benefits; step up and be a contributor too. Whether you're tossing out your code snippets , hammering out some tech blogs, or getting your hands dirty with maintaining and sprucing up open - source projects, every little thing you do might just end up being a massive force that pushes tech forward. And guess what? The Eastern FairyAlliance is this awesome place where we all come together. We're gonna team up and explore the whole silicon - based life thing, and in the process, we'll be fueling the growth of technology

相关推荐
间歇性努力持续性发呆的野生快乐选手1 小时前
stl容器使用和算法简介
数据结构·c++
Buke..2 小时前
【APP 逆向】哔哩哔哩 sign 参数逆向(下):OLLVM 混淆还原 sign 算法
java·javascript·爬虫·python·算法
闲研随记2 小时前
【文献阅读 ICLR 2026】RL算法:DECS
算法·llm·强化学习·iclr·rl
量化小c2 小时前
一行代码查 BTCUSDT 和 AAPL 最新价?QuantDash 统一多市场实时行情接口实战
后端·算法·github
沙盘客3 小时前
AFSIM 官方案例库全景与解读方法论
c++·经验分享·后端
机器人梦想家3 小时前
具身机器人视觉服务的异步回调与并发控制
数据库·算法·机器人
_wxd6664 小时前
STL-stack与queue与priority_queue(容器适配器)
c++
我命由我123454 小时前
人脸识别 - 人脸识别选帧
java·人工智能·python·算法·安全·java-ee·人脸识别
时针滴滴答啊4 小时前
最大子数组和
算法·leetcode·职场和发展
zh路西法4 小时前
【玩转VLA具身智能机械臂】(二):MoveIt2 运动规划——架构、配置与 C++ 上手
c++·机械臂·路径规划·vla·planner·moveit2