代码:字符串中连续与重复字符判断

来源:互联网 发布:火箭22连胜麦迪数据 编辑:程序博客网 时间:2024/05/17 03:50


字符串操作:


int consecutiveCharMaxCount( char *str)
{
char *pstr = NULL;
char *p = NULL;
int value = 0;
int incN = 1;
int decN = 1;
int maxCount = 0;
char chrp = 0;
char chrn = 0;


if(str == NULL)
return ERROR;
        p = str;
        while(*p != '\0')
        {
                pstr = p;


 incN = 1;
 decN =1;
 value = 0;
                while(*pstr != '\0')
                {
chrp = *pstr;
chrn = *(++pstr);
                        value = chrp - chrn;
                        if(value == 1)
                                incN++;
                        else
                        {
                                maxCount = (maxCount>=incN)?maxCount:incN;
                                incN = 1;
                        }
    
                        if(value == -1) 
                                decN++;
                        else
                        {
                                maxCount = (maxCount>=decN)?maxCount:decN;
                                decN = 1;
                        }


                }
                p++;
        }


return maxCount;
}


int repeatedCharMaxCount( char *str)
{
        char pchar[2] = {0};
        char *pindex = NULL;
        char *pstr = NULL;
        int n = 0;
        int maxCount = 0;

if(str == NULL)
return ERROR;
        pstr = str;
        while(*pstr != '\0')
        {   
                memset(pchar,0,2);
                pchar[0] = *pstr;
                pindex = index(pstr,pchar[0]);
                n = strspn(pindex,pchar);
                maxCount = (maxCount>=n)?maxCount:n;
                pstr++;
        }   

        return maxCount;
}



1 0
原创粉丝点击