// sameCharForMore.cpp : Defines the entry point for the console application.
// 编写一个 C 函数,该函数在一个字符串中找到可能最长子字符串,
// 该字符串是由同一字符组成的
#include "stdafx.h"
#include "iostream"
#include "cassert"
using namespace std;
typedef struct chNode {
char* chPointer;
int count;
} CHNODE;
CHNODE FindMoreSameChars(char* src)
{
assert(*src);
int curCount=1;
CHNODE nodeHaveFound;
if (*src) {
nodeHaveFound.chPointer =src;
nodeHaveFound.count =1;
}
while (*src++) {
if (*src ==*(src-1)) {
curCount++;
}
else {
if (curCount >nodeHaveFound.count) {
nodeHaveFound.chPointer =src-curCount;
nodeHaveFound.count =curCount;
}
curCount =1;
}
}
return nodeHaveFound;
}
int main(int argc, char* argv[])
{
CHNODE strNode;
char ch;
char* strForFind ="adfdddddllllfffffffflsjdfierrrrrrrrrrrrrrrlsdfls";
strNode =FindMoreSameChars(strForFind);
ch =*strNode.chPointer;
cout<<ch<<"\t"<<hex<<(int)strNode.chPointer<<endl
<<dec<<strNode.count<<endl;
//FindMoreSameChars("");
system("pause");
return 0;
}
/* 对于在栈中的字符串指针charPointer 指向堆区(new),要输出指针当前的字符,*charPointer
* 输出指针地址用 &charPointer,要输出指针指向的地址用 (int)charPointer
*/