c语言去除字符串中字母,C语言经典例题100例——C语言练习实例32解答(在字符串中删除指定字母或字符串)...
題目:刪除一個字符串中的指定字母,如:字符串 "aca",刪除其中的 a 字母。
程序分析:無。
程序源代碼:
// 百寶箱工作室 官方網址 http://www.baibaox.com
// 百寶箱工作室業務介紹 http://www.baibaox.com/BusinessIntroduction.html
#include
#include
#include
// 刪除字符串中指定字母函數
char* deleteCharacters(char * str, char * charSet)
{
int i;
int hash [256];
int currentIndex = 0;
if(NULL == charSet)
return str;
for(i = 0; i < 256; i++)
hash[i] = 0;
for(i = 0; i < strlen(charSet); i++)
hash[charSet[i]] = 1;
for(i = 0; i < strlen(str); i++)
{
if(!hash[str[i]])
str[currentIndex++] = str[i];
}
str[currentIndex] = '\0';
return str;
}
main()
{
char s[20] = " "; // 要刪除的字母,這里刪除空格
char s2[50] = "Object Oriented Programming."; // 目標字符串
printf("%s\n", deleteCharacters(s2, s));
}
以上實例運行結果如下:
ObjectOrientedProgramming.
總結
以上是生活随笔為你收集整理的c语言去除字符串中字母,C语言经典例题100例——C语言练习实例32解答(在字符串中删除指定字母或字符串)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【渝粤教育】国家开放大学2018年春季
- 下一篇: QuickTester对象,对象侦测器和