#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char* squeeze(char s1[] ,char s2[]){
int i,j,k = 0;
static char s[MAXSIZE];
for(i = 0; s1[i] != '\0' ;i++){
for(j = 0 ; s2[j] != s1[i] && s2[j] != '\0';j++)
;
if(s2[j] == '\0')
s[k++] = s1[i];
}
s[k] = '\0';
return s;
}
int main(int argc, char *argv[]) {
int i,j;
char a[MAXSIZE],p[MAXSIZE],*b;
printf("please input an string:\n");
scanf("%s",&a);
printf("please input the pattern:\n");
scanf("%s",&p);
b = squeeze(a,p);
printf("after:the test:%s\n",b);
}
这是指针返回字符串的简单实现。
返回一个数组的首字母,函数实际上返回的是数组的开始地址,在主函数中用一个指针去获取就可以
注意!!!
因为所返回的是函数局部变量的地址,而局部变量在函数被调用完后即被销毁,此时主函数拿到的地址就变成了一个无效的地址,所以在函数中我们要把数组用static限定,使他的作用域扩展到整个空间