您的位置:首页 > 其它

利用哈希表进行字符处理的两个例子

2016-07-28 21:05 183 查看
输入两个字符串,从一个字符串中删除第二个字符串中的字符,用一个哈希表记录了第二个字符串中的字符

void delete_ch(char *src, char *del_chs) {
int hash[256];
int i;
char *p_slow, *p_fast;
int del_len = strlen(del_chs);
p_slow = src;
p_fast = src;
memset(hash, 0, sizeof(int) * 256);
for (i = 0; i < del_len; i++)
hash[del_chs[i]] = 1;
while (*p_fast) {
if (hash[*p_fast] == 1) {
p_fast++;
}
*p_slow++ = *p_fast++;
}
*p_slow = '\0';
}


定义一个函数,删除字符串中所有重复出现的字符,仍然利用哈希表记录字符串中已经出现的字符

void delete_repeatc(char *src)
{
if (src == NULL)
return;
int hashtable[256];
for (int i = 0; i < 256; i++)
hashtable[i] = 0;
char *fast = src;
char *slow = src;
while (*fast != '\0')
{
if (hashtable[*fast] == 0)
{
*slow = *fast;
hashtable[*fast] = 1;
slow++;
}
fast++;
}
*slow = '\0';
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: