/*編寫函數replace(char *s,char c1,char c2)實現將s所指向的字符串中全部字符c1用c2替換,字符串、字符c1和c2均在主函數中輸入,將原始字符串和替換後的字符串顯示在屏幕上*/函數
#include <stdio.h> void replace(char *s,char c1,char c2) { for (int i = 0; s[i] != '\0'; i++) { if (s[i] == c1) { s[i] = c2; } } } int main(int argc, const char * argv[]) { char str[100]; char c1, c2; printf("請輸入一段字符串\n"); gets(str); printf("請輸入須要替換的字符\n"); scanf("%c",&c1); printf("請輸入替換後的字符\n"); //注意:清理輸入緩衝區 setbuf(stdin,NULL); scanf("%c",&c2); printf("原字符串爲:%s\n",str); replace(str, c1, c2); printf("替換後的字符串爲:%s\n",str); return 0; }