華為面試題答案找出最大長度子字符串
更新時間:2013年12月17日 08:56:40 作者:
找出最大長度子字符串,打印并且返回長度。 例如 str = "abc123abcd234abcdefgha324adsdawqdasdaseqqwe345abchded",看下面的代碼實現(xiàn)吧
復(fù)制代碼 代碼如下:
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;
}
相關(guān)文章
C++中的三種繼承public,protected,private詳細(xì)解析
我們已經(jīng)知道,在基類以private方式被繼承時,其public和protected成員在子類中變?yōu)閜rivate成員。然而某些情況下,需要在子類中將一個或多個繼承的成員恢復(fù)其在基類中的訪問權(quán)限2013-09-09
數(shù)據(jù)結(jié)構(gòu)串的操作實例詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)串的操作實例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07

