每日一题——第九十二题

// JudgePalindromicString.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

//

题目:判断控制台输入的一串字符串是否为回文字符串

c 复制代码
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
bool isPalindromicString(const char* str);

int main() {

	char str[100];
	printf("请输入一个字符串:");
	fgets(str, sizeof(str), stdin); // 使用fgets代替scanf,避免缓冲区溢出
	str[strcspn(str, "\n")] = 0;//去除字符串末尾的换行符
	if (isPalindromicString(str)) {
		printf("字符串 '%s' 是回文字符串\n", str);
	}
	else {
		printf("字符串 '%s' 不是回文字符串\n", str);
	}

	return 0;
}

bool isPalindromicString(const char* str) {
	int low = 0;
	int high = strlen(str) - 1;
	while (low < high) {

		if (str[low] != str[high]) {
			return false;
		}

		low++;
		high--;
	}

	return true;//对比循环结束,均相等,则返回true
}
相关推荐
小七在进步8 小时前
数据结构:线性表之单链表
c语言·数据结构
鱼子星_8 小时前
【数据结构】排序的拓展——快速排序的生态多样性与归并排序沾染文件操作
c语言·数据结构·算法
dtq04249 小时前
C语言刷题函数2 - 用函数实现数组操作
c语言·开发语言
芯岭技术郦9 小时前
MS32C001‑C:极致成本 32 位 MCU
c语言·开发语言·单片机
玖玥拾18 小时前
C/C++ 基础笔记(十四)多态与模板编程
c语言·c++·多态·模板
caimouse1 天前
Reactos 第1章 概述
c语言·开发语言·架构
啊森要自信1 天前
【GUI自动化测试】控件、鼠标键盘操作与多场景自动化
c语言·开发语言·python·adb·ipython
lpl3129055091 天前
skynet 共享数据原理
服务器·c语言·lua
hahjee1 天前
【鸿蒙PC】kcp 移植:AtomCode Skills 4 步速通单文件 C 库适配
c语言·华为·harmonyos
AI科技星1 天前
《数术工坊:非欧射影录》类型:硬核光影·几何本源
c语言·开发语言·网络·量子计算·agi