1. 编程学习网 > 编程教程 > C语言教程 > C语言100道经典题目32

C语言100道经典题目32

C 语言经典100例

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

程序分析:无。

实例

#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

本文由IT教学网整理发布,转载请注明出处:http://www.clang.cc//jiaocheng/cyuyan/1206.html

联系我们

在线咨询:点击这里给我发消息

咨询电话:400-998-2681

工作时间:7*24小时无休