回文字符串的判定------非递归与递归实现(未完)
2014-04-19 07:49
337 查看
检测一个字符串时候具有回文特性,回文特性的字符包括字母(同一字母的大小写等价)和数字,并且可以忽略空格和标点符号。
1.非递归实现
这里非递归实现的思想就是遍历字符串,将字符串strsrc[]的字母和数字赋给另一个字符数组str(目的是为了消除其他标号对判断回文的干扰),然后判断str[]是否是回文,它和源字符串strsrc的回文特性是一致的。
代码如下:
1.非递归实现
这里非递归实现的思想就是遍历字符串,将字符串strsrc[]的字母和数字赋给另一个字符数组str(目的是为了消除其他标号对判断回文的干扰),然后判断str[]是否是回文,它和源字符串strsrc的回文特性是一致的。
代码如下:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 20 void TestPalindrome(const char *strsrc); int main(void) { char *strsrc=(char *)malloc(sizeof(char)*MAX); printf("input the string you want to test:\n"); gets(strsrc); TestPalindrome(strsrc); return 0; } void TestPalindrome(const char *strsrc) { char *p1=(char *)malloc(sizeof(char)*MAX); char *str=(char *)malloc(sizeof(char)*MAX); int j,i=0; if(NULL==p1 || NULL==str) { printf("insufficient memory!\n"); exit(0); } strcpy(p1,strsrc); while(*p1 !='\0') { if(*p1>='A' && *p1<='Z')//'A'~'Z'---->'a'~'z' *p1=*p1+'a'-'A'; if((*p1>='a' && *p1<='z') || (*p1>='0' && *p1<='9')) { str[i]=*p1;//将源字符串的字母和数字赋给字符串数组src[] i++; } p1++; } for(j=0;j<i/2;j++) { if(str[j] !=str[i-j-1]) break; } if(j>=i/2) printf("该字符串为回文序列!\n"); else printf("该字符串不是回文序列!\n"); }
相关文章推荐
- 回文字符串判断---递归实现
- Python用递归实现回文字符串的判断
- 用递归实现判断一个字符串是否是回文的方法
- 用递归实现判断一个字符串是否是回文的方法
- 递归实现回文字符串判断
- 用递归思想实现字符串反转
- 求字符串长度之递归与非递归的C语言实现
- 算法:实现链表存储的回文字符串判断
- 用递归实现字符串的逆置
- 编写一个函数reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。
- 用递归的方法实现输入输出回文
- 字符串的全排列非递归实现算法
- 回文--递归实现
- 每天一个JavaScript实例-递归实现反转数组字符串
- 多种方式实现字符串/无符号数反向输出_栈_递归_头尾指针
- 回文 用递归实现 java
- 递归实现 参数字符串中的字符反向排列
- [小算法] 递归实现回文判断
- Java递归实现字符串的倒序输出
- 用递归实现字符串的逆置