删除一个字符串中出现次数最少的字符
2013-07-28 12:14
288 查看
** 题目:删除一个字符串中出现次数最少的字符,函数原型为:
**
** 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);
}
**
** 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);
}
相关文章推荐
- 【C编程练习】华为2013校园招聘机试题目题4:删除一个字符串中出现次数最少的字符,函数原型为:char * delChar(char *s,int iLen)
- 删除一个字符串中出现次数最少的字符
- 【华为OJ】删除一个字符串中出现次数最少的字符
- 删除一个字符串中出现次数最少的字符
- 删除一个字符串中出现次数最少的字符
- 华为机试——删除一个字符串中出现次数最少的字符
- 华为2013校园招聘机试题目题4:删除一个字符串中出现次数最少的字符
- 编写一个函数,删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。
- 华为2013校园招聘上机笔试题-删除一个字符串中出现次数最少的字符
- 神州数码:输入一个字符串: (1)输出出现次数最少的字符 (2)删除出现 4000 次数最少的字符并输出字符串
- 删除字符串中出现次数最少的字符
- 哈希表对字符串的高效处理7:删除字符串中出现次数最少的字符
- HWOJ 删除字符串中出现次数最少的字符
- 华为 oj 简单密码破解&&汽水瓶&&删除字符串中出现次数最少的字符&&字符串排序
- 删除字符串中出现次数最少的字符
- 函数、数组区别以及函数注意点和回调函数,以及删除字符串中出现次数最少的字符的程序
- [华为上机练习题]7.删除字符串中出现次数最少的字符
- Java字符串题目--删除给定字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符
- 删除字符串中出现次数最少的字符