在制定字符串内删除指定字符。

来源:互联网 发布:专业网络k歌声卡 编辑:程序博客网 时间:2024/06/06 19:38
char* func(char *str, char ch){size_t size = strlen(str), count = 0;for (size_t i = 0; i < size; i++){if (str[i] == ch){++count;}}if (count){char *pc = new char[size + 1 - count];int temp = 0, num = 0;while (str[num] != '\0'){if (str[num] != ch){*(pc + temp) = str[num];++temp;}++num;}memset(str, 0, size);strcpy(str, pc);delete[] pc;}return str;}