Valid Palindrome 判断回文字符串
2015-06-13 12:02
232 查看
特别注意:
1. 单个字符也是回文,例如 “a”
2. 字符和数字都是有效的,例如 “1a1”是回文
1. 单个字符也是回文,例如 “a”
2. 字符和数字都是有效的,例如 “1a1”是回文
#include <stdio.h> #include <stdbool.h> bool isValidIndex(int ulHead, int ulTail, const int LEN) { if(ulHead < ulTail && ulHead < LEN && ulHead >= 0 && ulTail < LEN && ulTail >= 0) return true; else return false; } bool isValidChar(char ucLetter) { if((ucLetter >= 'A' && ucLetter <= 'Z') || (ucLetter >= 'a' && ucLetter <= 'z') || (ucLetter >= '0' && ucLetter <= '9')) return true; else return false; } bool isSame(char ucHead, char ucTail) { /*定义局部变量*/ char cmpHead; char cmpTail; /*非字母则退出*/ if( (!isValidChar(ucHead)) || (!isValidChar(ucTail)) ) return false; /*字符转换为小写*/ if(ucHead >= 'A' && ucHead <= 'Z') cmpHead = ucHead-'A'+'a'; else cmpHead = ucHead; if(ucTail >= 'A' && ucTail <= 'Z') cmpTail = ucTail-'A'+'a'; else cmpTail = ucTail; /*比较字符*/ if(cmpHead == cmpTail) return true; else return false; } bool isPalindrome(char* s) { /*空字符串为回文字符串*/ if(s == 0 || strlen(s) == 0 || strlen(s) == 1) return true; /*定义局部变量*/ int ulHead = 0; int ulTail = strlen(s) - 1; const int LEN = strlen(s); bool result = true; while(isValidIndex(ulHead,ulTail,LEN)) { if(!isValidChar(s[ulHead])) { ulHead++; continue; } if(!isValidChar(s[ulTail])) { ulTail--; continue; } /*判断字符是否相等*/ if( !isSame(s[ulHead], s[ulTail]) ) { result = false; break; } else { ulHead++; ulTail--; } } return result; } int main() { char *s = "1r 1"; int result = isPalindrome(s); printf("%d\n", result); }
相关文章推荐
- C语言的左位移能不能超过8位?
- kettle初探
- Linux下安装Memcached图解教程
- 强连通分量分解
- First Missing Positive
- mysql win源码比较大 不需要的文件删除 记录下来
- 每天睡6小时和8小时区别有多大,看完再也不敢熬夜了
- 微软2014校园招聘笔试试题
- java异常概述
- 九鼎创展s5p4418开发板Android4.4-文件系统编译脚本分析
- beego模板语法 go语言模版语法
- epoll模型的理解封装与应用
- malloc 函数和new的区别
- 关于spring 整合 hibernate 事务不回滚
- Android Handler机制
- C#长链接转短链接(调用新浪api)
- MMC在一个管理单元检测到错误
- DB2常用
- 创始人和VC疯狂撕逼,究竟所为何事?
- 《你的灯亮着吗》阅读笔记(三)