C 练习实例32

C 语言教程 · 2019-02-21 19:57:20

题目:删除一个字符串中的指定字母,如:字符串 "aca",删除其中的 a 字母。

程序分析:无。

实例

// Created by www.codercto.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[i] = 0; for(int i = 0; i < strlen(charSet); i++) hash[charSet[i]] = 1; int currentIndex = 0; for(int i = 0; i < strlen(str); i++) { if(!hash[str[i]]) str[currentIndex++] = str[i]; } str[currentIndex] = '\0'; return str; } int main() { char s[2] = "a"; // 要删除的字母 char s2[5] = "aca"; // 目标字符串 printf("%s\n", deleteCharacters(s2, s)); return 0; }

以上实例输出结果为:

c

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Programming the Mobile Web

Programming the Mobile Web

Maximiliano Firtman / O'Reilly Media / 2010-07-23 / $44.99

* Learn how to use your existing skills to move into mobile web development * Discover the new possibilities of mobile web development, and understand its limitations * Get detailed coverage of ......一起来看看 《Programming the Mobile Web》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具