C 练习实例32

3835 1
LZH实名认证 2022-3-18 20:51:04 | 显示全部楼层 |阅读模式
题目:删除一个字符串中的指定字母,如:字符串 "aca",删除其中的 a 字母。

程序分析:无。

实例
//  Created by www.runoob.com on 15/11/9.
//  Copyright © 2015年 菜鸟教程. All rights reserved.
//

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// 删除字符串中指定字母函数
char* deleteCharacters(char * str, char * charSet)
{
    int hash [256];
    if(NULL == charSet)
        return str;
    for(int i = 0; i < 256; i++)
        hash = 0;
    for(int i = 0; i < strlen(charSet); i++)
        hash[charSet] = 1;
    int currentIndex = 0;
    for(int i = 0; i < strlen(str); i++)
    {
        if(!hash[str])
            str[currentIndex++] = str;
    }
    str[currentIndex] = '\0';
    return str;
}

int main()
{
    char s[2] = "a";     // 要删除的字母
    char s2[5] = "aca";  // 目标字符串
    printf("%s\n", deleteCharacters(s2, s));
    return 0;
}
以上实例输出结果为:

c
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行