删除字符串中指定的字符,比如字符串“hello hello hello”,删除字符‘l’。
char * delCh(char * str, char ch)
{
int i, j;
if (str == NULL)
{
return NULL;
}
for (i = j = 0; str[i] != '\0'; i++)
{
if (str[i] != ch)
{
str[j] = str[i];
j++;
}
}
str[j] = '\0';
return str;
}
验证程序:
#include <stdio.h>
int main()
{
char str[64] = "hello hello hello";
char ch = 'l';
printf("%s\n", delCh(str, ch));
return 0;
}
输出结果: