題目:洗掉一個字串中的指定字母,如:字串 "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] = '\0'; 22 return str; 23 } 24 25 int main() 26 { 27 char s[2] = "a"; // 要洗掉的字母 28 char s2[5] = "aca"; // 目標字串 29 printf("%s\n", deleteCharacters(s2, s)); 30 return 0; 31 }
以上實體輸出結果為:
c
感謝你的閱讀,請用心感悟!希望可以幫到愛學習的你!!分享也是一種快樂!!!請接力,,,
點擊查看原文,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/17352.html
標籤:C
上一篇:C 實戰練習題目31
下一篇:9.C語言 格式控制符/占位符
