//函数定义;结构体变量成员的表示方法
#include <stdio.h>
#include <string.h>
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
struct student fun(struct student a)//结构体定义函数类型struct student
{ int i;
a.sno = 10002;
/**********found**********/
strcpy(a.name, "LiSi");//将结构体中姓名换成LiSi
/**********found**********/
for (i=0; i<3; i++) a.score[i]+= 1;//将结构体中成绩加一
return a;
}
void main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, t;
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
t = fun(s);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);
printf("\n");
}
//将字符串数组中M各字符串合并组成一个字符串
void fun(char a[M][N],char *b)
{
int k = 0;
for (int i = 0; i < M; i++)//M个字符串,按顺序存入一个新的字符串
{
for (int j = 0; a[i][j] != '\0'; j++)//遍历字符串中每一个字符
b[k++] = a[i][j];
}
b[k] = '\0';//重要,在字符串最后加入字符串标记符
}
//从字符串中找出子字符串的方法
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#define N 80
int fun(char *s,char *t)
{ int n;
char *p, *r;
n=0;
/*************found**************/
r=t;
while ( *s )
{
p = s;
while ( *r )
{
if ( *r == *p )
{
r++;
p++;
}
else
break;
if ( *r == '\0' )
n++;
}
/*************found**************/
r=t;
s++;
}
return n;
}
void main()
{char a[N],b[N]; int m;
printf("\nPlease enter string a: ");
gets(a);
printf("\nPlease enter substring b: ");
gets(b);
m=fun(a,b);
printf("\nThe result is :m=%d\n",m);
}