您的位置:首页 > 其它

删除一个字符串中出现次数最少的字符

2013-07-28 12:14 288 查看
** 题目:删除一个字符串中出现次数最少的字符,函数原型为:

**

** char * delChar(char *s,int iLen)

**

** 其中 s为输入字符串,iLen为输入字符串长度。

**

** 如输入字符串为“abcdd”,输出为"dd"。

**

** 字符串中只有小写字母,不含空格且字符串最大长度不超过20。

**/

/**
**
** 题目:删除一个字符串中出现次数最少的字符,函数原型为:
**
**       char *  delChar(char *s,int iLen)
**
**		 其中 s为输入字符串,iLen为输入字符串长度。
**
**	     如输入字符串为“abcdd”,输出为"dd"。
**
**		 字符串中只有小写字母,不含空格且字符串最大长度不超过20。
**/

方法一:

[cpp] view plaincopyprint?

#include <stdio.h>

#include <string.h>

#include <malloc.h>

char * delChar(char *s,int iLen)

{

if((s == NULL) || iLen <= 0)

{

return NULL;

}

int i;

/*定义能够长度为26的数组*/

const int MAXLEN = 26;

/*min表示字符串中字符最少的数目*/

/*数组nCountTable分别存储当前字符串中存在的字符数目,不存在则为0*/

unsigned int min,nCountTable[MAXLEN];

/*初始化为0*/

for(i = 0;i < MAXLEN;i ++)

{

nCountTable[i] = 0;

}

/*统计当前字符串中各个字符的数目*/

for(i = 0;i < iLen;i ++)

{

nCountTable[*(s + i) - 'a'] ++;

}

/*把nCountTable数组中第一个不为0的,作为第一个数,用于找出最少的个数*/

while(nCountTable[i] == 0)

{

i ++;

}

min = nCountTable[i];

/*找出字符数目最少的那个数,不存在的不算入其中*/

for(i = 0;i < MAXLEN;i ++)

{

if(nCountTable[i] != 0)

{

if(nCountTable[i] < min)

{

min = nCountTable[i];

}

}

}

/*删除字符串中最少的字符,并且返回*/

char *pSlow = s;

char *pFast = s;

while(*pFast != '\0')

{

if(nCountTable[*pFast - 'a'] != min)

{

*pSlow = *pFast;

pSlow ++;

}

pFast ++;

}

*pSlow = '\0';

return s;

}

int main()

{

char str[] = "abadccdehigiktk";

int iLen = strlen(str)/sizeof(char);

char *tmp = delChar(str,iLen);

printf("%s\n",tmp);

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