求字符串最长对称字符串的长度(转何海涛博客)

自己实现的代码:

 

#include "stdafx.h" #include "stdio.h" #include "string.h" int get_maxlen(char *str){ char * current=str; int max_len=1; int temp_len=1; while(*current){ //奇数长度对称字符串处理 char * begin=current-1; char * end=current+1; while(begin>=str && *end && *begin==*end){ begin--; end++; } temp_len=end-begin-1; if(temp_len>max_len) max_len=temp_len; //偶数长度对称字符串处理 begin=current; end=current+1; while(begin>=str && *end && *begin==*end){ begin--; end++; } temp_len=end-begin-1; if(temp_len>max_len) max_len=temp_len; current++; } return max_len; } void main(){ char * str="woshioihsow"; printf("%d/n",get_maxlen(str)); } 

你可能感兴趣的:(C/C++实现,数据结构和算法)