//C和C指针第六章Page115
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//第一题
char * find_char(char const * source, char const * chars){
if (source == NULL || chars == NULL)
return NULL;
char *s1, *s2;
for (s1 = source; *s1 != '\0'; s1++){
for (s2 = chars; *s2 != '\0'; s2++){
if (*s1 == *s2){
return s1;
}
}
}
}
//第二题
int del_substr(char *str, char const *substr){
if (str == NULL || substr == NULL)
return 0;
char * s1 = NULL, *s2 = NULL;
while (*str != '\0')
{
int nLenth = 0;
s1 = str;
s2 = substr;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
nLenth++;
s1++;
s2++;
}
if (*s2 == '\0'){ //全匹配的标志
s2 = s1; //s2指针移动向s1的位置
s1 -= nLenth; //s1指针移动向匹配开始的位置。
while (*s1 != '\0'){
if (*s2 == '\0'){ //判断s2是否已经到了结尾的位置,如果是,s2指针停止移动,防止s2指向不可预知的位置;
*s1 = '\0';
s1++;
}
else{
*s1 = *s2;
s1++;
s2++;
}
}
return 1;
}
str++