華為面試題答案找出最大長度子字符串
更新時間:2013年12月17日 08:56:40 作者:
找出最大長度子字符串,打印并且返回長度。 例如 str = "abc123abcd234abcdefgha324adsdawqdasdaseqqwe345abchded",看下面的代碼實現(xiàn)吧
復制代碼 代碼如下:
int findMaxSubstring(char* str)
{
int maxLength = 0;
int maxStartIndex = 0;
int curLength = 0;
int curStartIndex = 0;
bool isFind = 0;
for(unsigned int i = 0;i<strlen(str);i++)
{
if(str[i] >= 'a' && str[i] <= 'z')
{
if(isFind == 0)
{
isFind = 1;
curLength = 1;
curStartIndex = i;
}
else
{
curLength++;
}
}
else if (str[i] < 'a' || str[i] > 'z')
{
isFind = 0;
if(curLength > maxLength)
{
maxLength = curLength;
maxStartIndex = curStartIndex;
curLength = 0;
}
}
}
char *p = NULL;
p = &str[maxStartIndex];
while(*p >= 'a' && *p <= 'z')
{
putchar(*p);
p++;
}
return maxLength;
}
相關文章
Qt基礎開發(fā)之QString與QByteArray詳細用法與區(qū)別及QString QByteArray互轉
這篇文章主要介紹了Qt基礎開發(fā)之QString與QByteArray詳細用法與區(qū)別及QString QByteArray互轉,需要的朋友可以參考下2020-03-03C語言字符串函數(shù)與內(nèi)存函數(shù)精講
這篇文章主要介紹一些c語言中常用字符串函數(shù)和內(nèi)存函數(shù)的使用,并且為了幫助讀者理解和使用,也都模擬實現(xiàn)了他們的代碼,需要的朋友可以參考一下2022-04-04