编写一个函数,删除一个字符串中的子串

来源:互联网 发布:足球阵型软件 编辑:程序博客网 时间:2024/05/21 09:17
/**/char source_string[] = "working hard and to be a diligent people!";char sub_string[]= "to be a diligent"; unsigned int cal_length(char *str){unsigned int length = 0;for(;*str != '\0';str++){length++;}return length;}int del_substr(char *str, char const *substr){char *p_str;char *p_substr;char *p_temp;p_str= str;p_substr= substr;assert( (p_str != NULL)&&(p_substr != NULL));//判断表达式的值,如果source为NULL,就打印出错消息printf("The origine str is:%s\n",p_str);printf("The sub str is:%s\n",p_substr); while(*p_str != *p_substr){p_str++;}p_temp = p_str + cal_length(p_substr);while(*p_substr++ != '\0'){if(*p_temp != '\0'){*p_str++ = *p_temp++; }else{*p_str = '\0';}}printf("The origine str changed is:%s\n",str);return 0;}int main(void){del_substr(source_string,sub_string);getch();return 0;}/**/

0 0
原创粉丝点击