题目:删除一个字符串中的指定字母,如:字符串 "aca",删除其中的 a 字母。
程序分析:无。
实例:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 // 删除字符串中指定字母函数 6 char* deleteCharacters(char * str, char * charSet) 7 { 8 int hash [256]; 9 if(NULL == charSet) 10 return str; 11 for(int i = 0; i < 256; i++) 12 hash[i] = 0; 13 for(int i = 0; i < strlen(charSet); i++) 14 hash[charSet[i]] = 1; 15 int currentIndex = 0; 16 for(int i = 0; i < strlen(str); i++) 17 { 18 if(!hash[str[i]]) 19 str[currentIndex++] = str[i]; 20 } 21 str[currentIndex] = ''; 22 return str; 23 } 24 25 int main() 26 { 27 char s[2] = "a"; // 要删除的字母 28 char s2[5] = "aca"; // 目标字符串 29 printf("%s ", deleteCharacters(s2, s)); 30 return 0; 31 }
以上实例输出结果为:
c
感谢你的阅读,请用心感悟!希望可以帮到爱学习的你!!分享也是一种快乐!!!请接力。。。