c语言程序设计教程牡丹答案,C语言程序设计练习题(含程序及参考答案)

C语言练习题

(所提供的源程序均采用自定义函数方式解决,如不采用函数方式,也可直接在main函数中借鉴该思想编程,因时间有限,所有程序未能一一验证,仅供参

考使用)

1、定义一个函数int fun(int a,int b,int c),它的功能是:若a,b,c能构成等边三角形函数返回3,若能构成等腰三角形函数返回2,若能构成一般三角形函数返回1,若不能构成三角形函数返回0。

#include stdio.h

int fun(int a,int b,int c)

{

if(a+b b+c a+cb)

{

if(a==b b==c )

return 3;

else if(a==b||b==c||a==c)

return 2;

else return 1;

}

else return 0;

}

void main()

{

int a,b,c,shape;

printf(nInput a,b,c:

scanf(%d%d%da,c);

printf(na=%d, b=%d, c=%dn,a,b,c);

shape =fun(a,b,c);

printf(nnThe shape : %dn,shape);

}

2、编写一个程序,将两个变量的值交换, 例如变量a中的值原为 3,b中的值原为8,程序运行后 a 中的值为8,b中的值为3。

#include stdio.h

void fun(int *x,int *y)

{

int t;

t=*x;*x=*y;*y=t;

}

void main()

{

int a=3,b=8;

printf(%d %dn

fun(a,

printf(%d %dn

}

3、从键盘输入3个数,将最大值放在变量a中,最小值放在变量c中。 例如,输入的数为:55 12 34,

输出结果应当是:a=55.0,b=34.0,c=12.0。

#include stdio.h

void fun(float *p,float *q,float *s)

{

float k;

if (*p*q)

{ k=*p;*p=*q;*q=k;}

if (*q*s)

{ k=*s;*s=*q;*q=k;}

if (*p*q)

{ k=*p;*p=*q;*q=k;}

}

void main()

{ float a,b,c;

printf(Input a b c: scanf(%f%f%fa,c); printf(a=%4.1f,b=%4.1f,c=%4.1fnn,a,b,c);

fun(b,

printf(a=%4.1f,b=%4.1f,c=%4.1fnn,a,b,c);

}

4、编写函数fun(int n),它的功能是:计算正整数n的所有因子(1和n除外)之和作为函数值返回。例如:n=120时,函数值为239。

#include stdio.h

int fun(int n)

{

int i,s=0;

for (i=2;ii++)

if (n%i==0) s=s+i;

return s;

}

void main() /*主函数*/

{

printf(%dn,fun(120));

}

5、计算s,并输出

1 1 1

S = 1 + ── + ─── + ?? + ──────

1+2 1+2+3 1+2+3+?+n

n通过键盘输入,例如:若n的值为11时,则函数值为:1.833333 #include stdio.h

#include stdlib.h

float fun(int n)

{

int i;

float s=1.0,h=1;

for (i=2;ii++)

{

h=h+i; s=s+1.0/h;

}

return s;

}

void main()

{

int n;float s;

printf(nPlease enter N:scanf(%dn);

s=fun(n);

printf(the result is:%f

}

10、判断两个整数m和n是否互质(即是否有公共的因子)(m≠1,n≠1)。方法是: 用2到t(t取m和n中较小的那个数)之间的数分别去除m和n,若m和n能同时被某个数除尽,则m和n不互质;否则它们互质。

例如,若输入187和85,则应输出No(表示它们不互质,它们有公因子17)。若输入89和187,则应输出Yes(表示它们互质)。

#include stdio.h

int IsThat( int m, int n )

{

int k, t, mk=1;

t=m;

if (m

for(k=2;kk++)

if( m%k==0 n%k==0 )

{ mk=0; break; }

return mk;

}

void main( )

{

int m, n;

printf( nPlease enter 2 numbers:n

scanf(%d %d, m,

if( IsThat( m, n ) )

printf( Yesn

else

printf( Non

}

11、将十进制正整数m转换成k进制数(2≤k≤9)并输出(m,k从键盘输入)。 例如,若输入8和2,则应输出1000(即十进制数8转换成二进制表示是1000)。 #include stdio.h

void Trans( int m, int k )

{

int aa[20], i,j;

for( i = 0; m; i++ )

{

aa[i] = m%k;

m /= k;

}

for( j=i-1; j j-- )

printf(%d, aa[j]);

}

void main()

{

int b, n;

printf( nPlease enter a number and a base:n

scanf(%d %d, n,

Trans( n, b );

}

12、统计从键盘输入的50个实数中有多少个正数、多少个负数、多少个零 #include stdio.h

void fun(int *zs,int *fs,int *l,float aa[])

{

}

void main()

{

} float num[50]; int i,czs,cfs,cl; czs=cfs=cl=0; printf( nPlease enter 50 float numbers :n for(i=0;ii++) scanf(“%f”,num[i]); fun(czs,cfs,cl,num); printf(n正数:%d ,负数:%d ,零:%d n,czs,cfs,cl); int i; for(i=0;ii++) { if(aa[i]0) (*zs)++; (*fs)++; (*l)++; if(aa[i]0) if(aa[i]==0) }

13、计算并输出方程X2+Y2=1989的所有整数解 #include stdio.h

void fun()

{

}

void main()

{

} printf(“方程x^2+y^2=1989的整数解为:n”); fun(); int x,y; for(x=1;xx++) for(y=1;yy++) if(x*x+y*y==1989) printf(“x=%d ,y=%d n”,x,y);

14、从键盘输入10个整数,求出其中的最大值。 #include stdio.h

int fun(int aa[])

{

}

void main()

{

} int num[10],i; printf(“请从键盘输入10个整数:n”); for(i=0;ii++) scanf(“%d”,num[i]); printf(“n最大的数是:%dn”,fun(num)); int m,i; m=aa[0]; for(i=1;ii++) return m; if(aa[i]m) m=aa[i];

15、从键盘输入n值,输出如右图形。(例如n=5时) #include stdio.h

void fun(int n)

{

int i,j; for(i=1;ii++)

{

}

}

void main()

{

}int n; printf(“n请输入图形的行数:”); scanf(“%d”, fun(n);

1689683550.jpg

for(j=1;jj++) printf(“%3d”,1); printf(“%3d”,j); for(j=2;jj++) printf(“n”);

16、使用函数的方式,计算如下公式的值。 1 1 1 A1=1,A2=──,A3=──,??An=──── 1+A1 1+A2 1+A(n-1) 例如:若n=10,则应输出: 0.618056。 #include stdio.h

float fun ( int n )

{

float A=1; int i;

for (i=1; i i++)

A = 1.0/(1+A);

return A ;

}

void main( )

{

int n ;

printf(nPlease enter n:

scanf(%d,

printf(A%d=%fn, n, fun(n) ) ; }

* 17、使用函数的方式,,按下面的公式计算并输出数列的第m项。 ┌ 2, m = 1; │ 3, m = 2; FFF( m ) = ┤ 5, m = 3; └ FFF(m - 3) + FFF(m - 1), m

例如,若输入整数 9,则应输出:47。

(不用做)

* 18、使用函数的方式,按以下递归公式求函数值

┌10 (n=1)

fun(n)=│

└fun(n-1)+2 (n1)

例如,当给n输入5时,函数值为18;当给n输入3时,函数值为14。

(不用做)

19、计算并输出当00.97时下列多项式的值, 直到|S(n)-S(n-1)|0.000001为止。

2378939317.jpg

例如,在主函数中从键盘给x输入0.21后,输出为:s=1.100000。 #include stdio.h

#include math.h

double fun(double x)

{

int n=1; /* 循环计数*/

/* 累计数*/ double sn=1;

double xn=1,xn1=0; /*x的n值,以及x的n-1值;*/

while(fabs(xn-xn1)=0.000001)/*绝对值是否合格*/

{

return sn;

}

void main()

{

double x,s;

printf(Input x: scanf(%lfx);

s=fun(x);

printf(s=%fn

} xn=xn*x*(0.5-n+1)/n; /*表达式分解以后xn=(xn-1)*x*(0.5-n+1)/n*/ n+=1; sn+=xn; } /*sn累加上xn*/

20、计算并输出s。

x^2 x^3 x^n

s = 1 + x + ── + ── + ?? + ──

2! 3! n!

n,x从键盘输入,例如,当n=10,x=0.3时,函数值为1.349859。 #include stdio.h

double fun(double x,int n)

{

int i;

double f=1.0,h=x;

double s=1;

s=s+h/f;

for (i=2;ii++)

{

f=f*i;h=h*x;

s=s+h/f;

}

return s;

}

void main()

{

printf(%fn,fun(0.3,10));

}

21、从键盘输入high,计算并输出high以内最大的10个素数之和。例如,输入high的值为100,则输出732。

#include stdio.h

int fun( int high )

{

int sum = 0, n=0, j, yes;

while ((high = 2) (n 10))

{

yes = 1;

for (j=2; j j++ )

if (high % j ==0 )

{

yes=0; break;

}

if (yes==1)

{

sum +=high; n++;

}

high--;

}

return sum ;

}

void main ( )

{

printf(%dn, fun (100));

}

22、请编写函数float fun(int n),它的功能是:返回n(包括n)以内能被5或9整除的所有自然数的倒数之和。例如,n=20,返回0.583333。注意:要求n的值不大于100。

#include stdio.h

double fun(int n)

{

int i;

double sum=0.0;

if(n=100)

{

for (i=1;ii++)

}

return sum;

}

void main()

{

int n; double s;

printf(nInput n:

scanf(%dn);

s=fun(n);

printf(nns=%fn

} if(i%5==0||i%9==0) sum+=1.0/i;

23、请编一个函数fun(int *a, int n, int *odd, int *even),函数的功能是分别求出数组中所有奇数之和以及所有偶数之和。形参n给出数组a中数据的个数;利用指针odd返回奇数之和,利用指针even返回偶数之和。

例如:数组中的值依次为: 1,9,2,3,11,6;则利用指针odd返回奇数之和24;利用指针even 返回偶数之和8。

#include stdio.h

#define N 20

fun ( int *a, int n, int *odd, int *even )

{

int i,sum_odd=0,sum_even=0;

for(i=0;ii++)

if(a[i]%2==0)

sum_even+=a[i];

else

sum_odd+=a[i];

*odd=sum_odd;

*even=sum_even;

}

void main( )

{

int a[N]={1,9,2,3,11,6}, i, n=6, odd, even;

printf ( The original data is : n

for ( i = 0; i i ++ ) printf ( %5d, *(a+i) );

printf(nn

fun ( a, n, odd,

printf ( The sum of odd numbers: %dn, odd );

printf ( The sum of even numbers: %dn, even );

}

24、编写计算三角形面积的程序,注意:将计算面积定义成函数float fun(int a,int b,int c)(a,b,c为三角形的三条边,返回三角形的面积),在主函数中调用fun

#include stdio.h

#include math.h

float fun(int a,int b,int c)

{

}

void main()

{

}int a,b,c; printf(“请输入三角形三条边:n”); scanf(“%d%d%d”,b, if(a+b b+c a+cb) printf(“三角形面积为:%.2f”,fun(a,b,c)); printf(“无法构成三角形”); else float p; p=(a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c));

25、编写程序,求E=1+1/1!+1/2!+??+1/n!,要求最后一项的值小于10-4 #include stdio.h

double fun(int n)

{

double m=1.0;

int i=1;

double p=1.0;

do{

m=m+1.0/p;

i++;

p=p*i;

} while(1.0/p

return m;

}

void main()

{

int n; double s;

printf(Input n: scanf(%dn);

s=fun(n);

printf(s=%fn

}

26、计算并输出给定数组(长度为9)中每相邻两个元素之平均值的平方根之和。 例如,给定数组中的9个元素依次为12.0、34.0、4.0、23.0、34.0、45.0、18.0、3.0、11.0,输出应为:s=35.951014。

#include stdio.h

#include math.h

double fun(double x[9])

{

int i ;

double avg=0.0,sum=0.0;

for (i=0;ii++)

{

avg=(x[i]+x[i+1])/2;

sum+=sqrt(avg);

}

return sum;

}

void main()

{

double s,a[9]={12.0,34.0,4.0,23.0,34.0,45.0,18.0,3.0,11.0}; int i;

printf(nThe original data is :n

for(i=0;ii++)printf(%6.1f,a[i]); printf(nn

s=fun(a);

printf(s=%fnn

}

27、将1到m之内(含m)能被7或11整除的所有整数放在数组a中。 例如,若输入m的值为50,则在数组a中的值为:

7 11 14 21 22 28 33 35 42 44 49 #include stdio.h

#define M 100

void fun ( int m, int *a , int *n )

{

int i,count=0;

for(i=1;ii++){

if(i%7==0 || i%11==0) {

a[count++]=i;

}

}

*n=count;

}

void main( )

{

int aa[M], n, k;

fun ( 50, aa,

for ( k = 0; k k++ )

if((k+1)%20==0) printf(n

else printf( %4d, aa[k] );

printf(n

}

28、请编写函数int fun(int m,int score[],int below[]),它的功能是:将低于平均分的人数作为函数值返回,并将低于平均分的成绩放在below数组中(m表示score的长度,score表示成绩)。例如,当score数组中的数据为:10、20、30、40、50、60、70、80、90时,函数返回4,below中的数据应为:10、20、30、40。

#include stdio.h

#include stdlib.h

int fun(int score[], int m, int below[])

{

int total=0;

int average=0;

int *p_below=below;

int i=0;

int j=0;

for(i=0;ii++){

total=total+score[i];

}

average=total/m;

for(i=0;ii++){

if(score[i]average){

*p_below=score[i];

p_below++;

j++;

}

}

*p_below=NULL;

return(j);

}

main( )

{ int i, n, below[9] ;

int score[9] = {10, 20, 30, 40, 50, 60, 70, 80, 90} ;

n = fun(score, 9, below) ;

printf( nBelow the average score are:

for (i = 0 ; i i++) printf(%d , below[i]) ;

}

29、用插入排序法将n个字符进行排序(降序)。(提示:插入法排序的思路是:先对数组的头两个元素进行排序,然后根据前两个元素的情况插入第三个元素,再插入第四个元素?)。

#define N 80

#include stdio.h

#include string.h

void insert(char *aa)

{

int i,j,n; char ch;

n=strlen(aa);

for(i=1;ii++)

{

ch=aa[i];

j=i-1;

while ((jaa[j]))

aa[j+1]=aa[j];

j--;

}

aa[j+1]=ch;

}

}

void main()

{ char a[N]=QWERTYUIOPASDFGHJKLMNBVCXZ

int i;

printf(The original string : %sn

insert(a);

printf(The string after sorting:%snn

} {

30、假定整数数组a中元素的值不重复。删除a中值为x的元素(x从键盘输入)。

#include stdio.h

#define N 20

fun(int *a,int n,int x)

{ int p=0,i;

a[n]=x;

while(x!=a[p])

p=p+1;

if(p==n)

return -1;

else

{

for(i=p;ii++)

a[i]=a[i+1];

return n-1;

}

}

void main()

{ int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;

n=10;

printf(The original data :n

for(i=0;ii++) printf(%5d,w[i]);

printf(nInput x (to delete):scanf(%dx);

printf(Delete : %dn

n=fun(w,n,x);

if (n==-1) printf(***Not be found!***nn

else

{ printf(The data after deleted:n

for(i=0;ii++) printf(%5d,w[i]);printf(nn

}

}

31、一个已按从小到大的顺序排好的数组a,编写程序,从键盘输入一个数x按原来排序的规律将它插入数组a中。

#include stdio.h

#define M 20

void InSort( int *n, int vv[], int k )

{

int i=0 , j;

while ( k vv[i] i *n ) i++;

if ( i*n )

for ( j=*n; j j-- ) vv[j] = vv[j-1];

vv[i] = k;

++*n;

}

void main()

{

int aa[M], i, k, m;

system(cls

printf( nPlease enter a number:n

scanf( %d,

printf( nPlease enter %d numbers:n

for( i = 0; i i++ )

scanf( %d, aa[i] );

printf( nPlease enter another number:n

scanf( %d,

InSort( m, aa, k );

for(k=0; k k++)

printf( %d, aa[k] );

}

32、使用数组的方法筛选出1-100中的素数 #include stdio.h

void fun ( int *pt )

{

int i,j,flag;

*pt=2;*pt++;

for(i=3;ii++)

{

flag = 1;

for(j=2;jj++)

if(i % j == 0)

flag = 0;

if( flag )

{

*pt = i;*pt++;

}

}

*pt=0;

}

void main( )

{

int *pointer,a[30];

pointer=a;

fun(pointer);

printf(The result is :n while(*pointer)

printf ( %d ,*pointer++ ); }

33、已知一个数组a中包括10个整数元素,从a中第二个元素起,分别将后项减前项之差存入数组b,并按每行3个元素输出数组b。

#include stdio.h

void fun(int a[],int b[])

{

int i; for(i=1;ii++) b[i-1]=a[i]-a[i-1]; for(i=0;ii++) { printf(“%3d”,b[i]);

if((i+1)%3==0)

}

}

void main()

{

}int a[10]={1,2,3,4,5,6,7,8,9,10}; int b[9]; fun(a,b); printf(“n”);

34、请编写函数fun,函数的功能是:将M行N列的二维数组中的字符数据按列的顺序依次放到一个字符串中。

例如,二维数组中的数据为:

W W W W

S S S S

H H H H

则字符串中的内容应是:WSHWSHWSH。

#include stdio.h

void fun(int (*s)[10], int *b, int *n, int mm, int nn) {

int x,y;

for(x=0;xx++)

for(y=0;yy++)

{b[x*mm+y]=*(s[y]+x);

(*n)++;

}

}

void main()

{

int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}},i,j; int a[100]={0}, n=0;

printf(The matrix:n

for(i=0; i i++)

{ for(j=0;j j++)printf(%3d,w[i][j]);

printf(n

}

fun(w,a,

printf(The A array:n

for(i=0;ii++)printf(%3d,a[i]);printf(nn

}

printf ( The original data is : n

for ( i=0; i i++ )

{ for ( j =0; j j++ ) printf( %6d, aa[i][j] ); printf (n

}

y = fun ( aa );

printf( nThe sum: %dn , y );

printf(n

}

for( j=0; j j++ )

printf ( %6d, t[i][j] );

printf(n

}

fun ( t, p );

printf( nThe result is:n

for ( k = 0; k k++ ) printf ( %4d , p[ k ] );

printf(n

}

37、请编写程序,实现B=A+A',即把矩阵A加上A的转置,存放在矩阵B中。计算结果在main函数中输出。

例如,输入下面的矩阵: 其转置矩阵为:

|1 2 3| |1 4 7|

|4 5 6| |2 5 8|

|7 8 9| |3 6 9|

程序输出:

|2 6 10|

|6 10 14|

|10 14 18|

#include stdio.h

void fun(int a[3][3],int b[3][3])

{

int i,j;

for(i=0;ii++)

for (j=0;jj++)

b[i][j]=a[i][j]+a[j][i];

}

void main()

{ int a[3][3]={{1,2,3},{4,5,6},{7,8,9}},t[3][3];

int i,j;

fun(a,t);

for(i=0;ii++) {

for(j=0;jj++)

printf(%7d,t[i][j]);

printf(n

}

}

38、编写程序删除字符串s中从下标k开始的n个字符(n和k从键盘输入)。 例如,字符串内容为:Hellollo World!,k中的值为:5,n中的值为:3,结果为:Hello World!。

#include stdio.h

#define N 80

void fun(char *a,int k,int n)

{

int i;i=k;

while (a[i-1]!='')

{ a[i]=a[i+n];

i=i+1;

}

}

void main()

{ char s[N]=Hellollo World!

int k,n;

printf(nThe original string:%sn

printf(Enter index ———————— k: ),scanf(%dk); printf(Enter number to delete —— n: scanf(%dn); fun(s,k,n);

printf(nThe string after deleted: %sn

NONO();

}

39、编写程序,从键盘输入字符串tt,将其中每个单词的首字符改为对应的大写字母,首字符后的字母都改为对应的小写字母。

例如,若输入字符串:ab cD bOYxy!,则输出字符串为:Ab Cd Boyxy!。 #include stdio.h

#include string.h

char* EXUL( char tt[] )

{

int isfirst = 1;

int i, length;

length = strlen( tt );

for(i = 0; i i++)

{

if (tt[i] = 'a' tt[i] = 'z')

{tt[i] = isfirst?(tt[i] + 'A' - 'a'):tt[i];isfirst=0;} else if (tt[i] = 'A' tt[i] = 'Z')

{tt[i] = isfirst?tt[i]:(tt[i] - 'A' + 'a');isfirst=0;} else

isfirst = 1;

}

return tt;

}

main()

{

int i;

char tt[81];

printf(nPlease enter a string:

gets( tt );

printf( nThe result string is:%sn, EXUL( tt ) );

}

40、编写程序,从键盘输入字符串tt,字符串中'a'到'z'26个字母各自出现的次数,并依次放在pp所指数组中。

例如,当输入字符串:abcdefgabcdeabc后,程序的输出结果应该是:3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

#include stdio.h

void fun(char *tt, int pp[])

{

char *p_tt=tt;

int i=0;

while(i26){

for(;*p_tt!=NULL;p_tt++){

switch(*p_tt){ case 'a':pp[0]++;break; case 'b':pp[1]++;break; case 'c':pp[2]++;break; case 'd':pp[3]++;break; case 'e':pp[4]++;break; case 'f':pp[5]++;break; case 'g':pp[6]++;break; case 'h':pp[7]++;break; case 'i':pp[8]++;break; case 'j':pp[9]++;break; case 'k':pp[10]++;break; case 'l':pp[11]++;break; case 'm':pp[12]++;break; case 'n':pp[13]++;break; case 'o':pp[14]++;break; case 'p':pp[15]++;break; case 'q':pp[16]++;break; case 'r':pp[17]++;break; case 's':pp[18]++;break; case 't':pp[19]++;break; pp[i]=0; i++;}

}

}

} case 'u':pp[20]++;break; case 'v':pp[21]++;break; case 'w':pp[22]++;break; case 'x':pp[23]++;break; case 'y':pp[24]++;break; case 'z':pp[25]++;break;

main( )

{ char aa[1000] ;

int bb[26], k, n ;

printf( nPlease enter a char string: scanf(%s, aa) ; fun(aa, bb ) ;

for ( k = 0 ; k k++ ) printf (%d , bb[k]) ;

printf( n

}

41、请编写程序,把字符串s中所有的字符前移一个位置,串中的第一个字符移到最后。

例如:原有的字符串为:Mn.123xyZ,则调用该函数后,串中的内容为:n.123xyZM。

#include stdio.h

#include string.h

#define N 81

fun ( char *s )

{

char b[N] ;

sprintf(b, %s%c, s + 1, *s) ;

strcpy(s, b) ;

}

void main( )

{

char a[ N ] ;

printf ( Enter a string : gets ( a );

printf ( The original string is : puts( a );

fun ( a );

printf ( The string after modified :

puts ( a );

}

42、编写程序,把字符串中的内容逆置。

例如:字符串中原有的内容为:abcdefg,输出:gfedcba。 #include string.h

#include stdio.h

#define N 81

void fun ( char *s )

{

}

void main( )

{ char a[N];

printf ( Enter a string : gets ( a );

printf ( The original string is : puts( a ); fun ( a );

printf(n

printf ( The string after modified : puts ( a );

}

length = strlen( s ); for (i = 0;i length / 2; i++) { tmp = s[i]; s[i] = s[length - i - 1]; s[length - i - 1] = tmp; } int i ,length; char tmp;

43、编写程序,依次取出字符串s1中所有数字字符,形成新的字符串s2。 #include stdio.h

void fun(char *s)

{

int i,j;

for(i=0,j=0; s[i]!=''; i++)

if(s[i]='0' s[i]='9')

s[j++]=s[i];

s[j]='';

}

void main()

{

char item[80];

printf(nEnter a string : gets(item);

printf(nnThe string is : %s

,item);

fun(item);

printf(nnThe string of changing is : %s

,item ); }

44、分别将a、b所指字符串中字符倒序,然后按排列的顺序交叉合并到c所指数组中,过长的剩余字符接在c所指数组的尾部。

例如,当a所指字符串中的内容为:abcdefg,b所指字符串中的内容为:1234时,则c所指数组中的内容应该为:g4f3e2d1cba;

#include stdio.h

#include string.h

void myswap( char *s )

{

char *sp, *st, ch;

sp = s;

st = s + strlen(s) - 1;

while ( sp = st )

{ ch = *sp; *sp = *st; *st = ch; sp++; st--; }

}

void fun( char *a, char *b, char *c )

{

char s1[100], s2[100] , *sa, *sb;

strcpy(s1,a);

strcpy( s2, b );

myswap (s1);

myswap(s2);

sa = s1 ; sb = s2 ;

while ( *sa || *sb )

{ if ( *sa ){ *c = *sa; c++; sa++; }

if ( *sb ){ *c = *sb; c++; sb++; }

}

*c = 0;

}

void main()

{

char s1[100], s2[100], t[200];

printf(nEnter s1 string : scanf(%s

printf(nEnter s2 string : scanf(%s

fun( s1, s2, t );

printf(nThe result is : %sn }

45、已知字符串a1和a2,各自存放一个已按字母顺序排好的字符串,编程合并二个字符串a3中,合并后仍保持字母顺序(如a1中存放:accel,a2中存放ilrz,则a3中为acceillrz)。

#include stdio.h

#define M 3

#define N 20

void fun(char a[M][N], char *b)

{

int i,j,d=0;

for(i=0;ii++)b[i]=0;

for(i=0,d=0;ii++)

for(j=0;*(a[i]+j);j++)

{

*(b+d)=*(a[i]+j);

d++;

}

}

void main()

{ char w[M][N]={AAAA,BBBBBBB,CC}, a[100];

int i ;

printf(The string:n

for(i=0; i i++)puts(w[i]);

printf(n

fun(w,a);

printf(The A string:n

printf(%sprintf(nn

}

46、定义一个函数char * fun(char * s),判断一个字符串s是否是回文,当字符串是回文时,函数返回字符串:yes!,否则函数返回字符串:no!。所谓回文即正向与反向的拼写都一样,例如:adgda。

#include stdio.h

#define N 80

int fun(char *str)

{

int b=1,n,i;

n=strlen(str);

for(i=0;ii++)

{if(str[i]!=str[n-1-i])

{b=0; break;}

}

if(i==n/2) b=1;

return b;

}

void main()

{ char s[N] ;

printf(Enter a string: gets(s) ;

printf(nn puts(s) ;

if(fun(s)) printf( YESn

else printf( NOn

}

* 47、已知学生的记录由学号和学习成绩构成,输入N名学生的数据,存入a结构体数组中。并输出成绩最高的学生记录。

(不用做)

* 48、已知学生的记录由学号和学习成绩构成,将N名学生的成绩放入一个带头节点的链表结构中,h指向链表的头节点,找出学生的最高分。

(不用做)

* 49、学生的记录由学号和成绩组成,输入N名学生的数据放入结构体数组s中,把分数最低的学生数据放在h所指的数组中,注意:分数最低的学生可能不止一个。

(不用做)

* 50、 统计person所指结构体数组中所有性别(sex)为M的记录的个数,存入变量n中

(不用做)