gets(str);
printf("Enter a&&b:\n");
scanf("%c,%c",&a,&b);
printf("%s\n",str);
fprintf(fp,"%s\n",str);
replace(str,a,b);
printf("The new string is----%s\n",str);
fprintf(fp,"The new string is----%s\n",str);
fclose(fp);
}
13、/*在一个字串s1中查找一子串s2,若存在则返回子串在主串中的起始位置 ,不存在则返回-1。*/
main()
{char s1[6]="thisis";char s2[5]="is";
printf("%d\n",search(s1,s2));
system("pause");
}
int search(char s1[],char s2[])
{int i=0,j,len=strlen(s2);
while(s1[i]){
for(j=0;j<len;j++)
if(s1[i+j]!=s2[j]) break;
if(j>=len)return i;
else i++;
}
return -1;
}
14、/*用指针变量输出结构体数组元素。*/
struct student
{
int num;
char *name;
char sex;
int age;
}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};
main()
{int i;
struct student *ps;
printf("Num \tName\t\t\tSex\tAge\t\n");
/*用指针变量输出结构体数组元素。*/
for(ps=stu;ps<stu+5;ps++)
printf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);
/*用数组下标法输出结构体数组元素学号和年龄。*/