華為面試題答案找出最大長度子字符串
更新時(shí)間:2013年12月17日 08:56:40 作者:
找出最大長度子字符串,打印并且返回長度。 例如 str = "abc123abcd234abcdefgha324adsdawqdasdaseqqwe345abchded",看下面的代碼實(shí)現(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)文章
Qt基礎(chǔ)開發(fā)之QString與QByteArray詳細(xì)用法與區(qū)別及QString QByteArray互轉(zhuǎn)
這篇文章主要介紹了Qt基礎(chǔ)開發(fā)之QString與QByteArray詳細(xì)用法與區(qū)別及QString QByteArray互轉(zhuǎn),需要的朋友可以參考下
2020-03-03
C語言字符串函數(shù)與內(nèi)存函數(shù)精講
這篇文章主要介紹一些c語言中常用字符串函數(shù)和內(nèi)存函數(shù)的使用,并且為了幫助讀者理解和使用,也都模擬實(shí)現(xiàn)了他們的代碼,需要的朋友可以參考一下
2022-04-04
Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)編程
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)編程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
2022-08-08 
