验证回文串

题目来源于力扣–https://leetcode-cn.com/

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

1、首先将字符串的字母转为小写字母。
2、遇到非字母或者数字跳过
3、比较字母或者数字是否相同

main函数代码

#include<stdio.h>
bool isPalindrome(char * s);
int main(){
	char s[] = "A man, a plan, a canal: Panama";
	bool isTrue = isPalindrome(s);
	printf("%d",isTrue);
	return 0;
}

验证是否回文串代码

bool isPalindrome(char * s){
	//判断字符串长度
	int length = 0;
	while(s[length]){
		length++;
	}
	
	//如果字符串为空 返回true
	if(length==0){
		return true;
	}
	
	//首先将大写字母转为小写字母
	for(int i=0;i<length;i++){
		if(s[i]>='A'&&s[i]<='Z'){
			s[i] = s[i]+32;
		}
	}
	
	// 首指针 
	int start = 0;
	// 尾指针 
	int end = length-1;
	while(start<end){
		// 不是字母或者数字跳过 
		while(!(s[start]>='a'&&s[start]<='z')&&!(s[start]>='0'&&s[start]<='9')&&start<end){
			start++;
		}
		// 不是字母或者数字跳过 
		while(!(s[end]>='a'&&s[end]<='z')&&!(s[end]>='0'&&s[end]<='9')&&start<end){
			end--;
		}
		// 不相等直接返回false 
		if(s[start]!=s[end]){
			return false;
		}
		//首指针往后走 
		start++;
		// 尾指针往前走 
		end--;
	} 
	return true;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值