找出最大长度子字符串(只包含字母),打印并且返回长度.
/** 找出最大长度子字符串(只包含字母),打印并且返回长度。 例如str= "abc123abcd234abcdefgha324adsdawqdasdaseqqwe345abchded" 最大子字符串是“adsdawqdasdaseqqwe” */ #include <stdio.h> #include <stdlib.h> #include <string.h> int FindMaxSubString( char * str); int FindMaxSubString( char * str) { int maxStartIndex=0; int curStartIndex=0; int maxLength=0; int curLength=0; int findflag=0; unsigned int i=0; for(i=0;i<strlen(str);i++) { if(str[i] >= 'a'&&str[i] <= 'z') { if(findflag==0) { findflag=1; curLength=1; curStartIndex=i; }else { curLength++; } } if(str[i]<'a'||str[i]>'z') { findflag=0; if(curLength>maxLength) { maxLength=curLength; maxStartIndex=curStartIndex; curLength=0; } } } char *p=NULL; p=&str[maxStartIndex]; printf("Result = "); while(*p >= 'a'&&*p <= 'z') { putchar(*p); p++; } printf("\n"); return maxLength; } void main(void) { char *str="abc123abcd234abcdefgha324adsdawqdasdaseqqwe345abchded"; int maxlenth=0; maxlenth=FindMaxSubString(str); printf("MaxSubString.Length=%d\n",maxlenth); }