您的位置:首页 > 其它

一道C机试题目

2013-08-08 20:10 323 查看
题目是这样的

求一个字符串中连续字母的个数

比如I have a book. : 1

I have a dog. : 0

I haavee aa dogg : 4

#include <windows.h>
#include <iostream>

using namespace std;

void GetDupStringCount( const char* pStr, int &iOut )
{
if( !pStr )
return;

int iLen = strlen( pStr );

if( !iLen )
return;

char cValue = *pStr;
int iNumCount = 0;//重复个数

iOut = 0;//设置为0
int iIndex = 0;//当前索引

while( iLen-- )
{
if( !( ( cValue >= 'a' && cValue <= 'z' ) || ( cValue >= 'A' && cValue <= 'Z' ) ) )
{
cValue = *( pStr + ++iIndex );

iNumCount = 0;

continue;
}

if( cValue == *( pStr + iIndex ) )
{
iNumCount++;
}
else
{
cValue = *( pStr + iIndex );

if( iNumCount > 1 )
{
iOut++;
iNumCount = 0;
--iIndex;
}

}

iIndex++;

}
}

int main( int argc, char* argv[] )
{
char szStr[] = " I haveee  a book!   ";

int iOut = -1;

GetDupStringCount( szStr, iOut );

cout << iOut << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: