一、分析程序,寫出結(jié)果
1.int a[]={1,2,3,4,5,6,7,8,9};
printf("%d\n",*(a+4));
2.int a[][3]={{1,2,3},{4,5,6},{7,8,9}},*p;
p=&a[0][0];
printf("%d\n",p[5]);
3.int a[5]={1,2,3,4,5},*p;
p=&a[0];
則*(p+1)=____,*(a+2)=______.
4.#include "stdio.h"
main()
{
int a[]={1,2,3,4,5,6,7,8,9,10,11,12};
int *p=a+5,*q=NULL;
*q=*(p+5);
printf("%d %d\n",*p,*q);
}
5.int a=25,*p;
p=&a;
printf("%d\n",++*p);
6.void fun(int *a,int b[])
{
b[0]=*a+6;
}
main()
{
int a=0,b[5];
b[0]=3;
fun(&a,b);
printf("%d\n",b[0]);
}
二、編程
1.從鍵盤讀入一串字符,遇到三個(gè)連續(xù)的'#'結(jié)束,統(tǒng)計(jì)其中英文字母的個(gè)數(shù)并輸出。
#include "stdio.h"
main()
{
char *p;
int letter=0;
gets(p);
while(*p)
{
if(*p=='#'&&*(p+1)=='#'&&*(p+2)=='#') break;
else if(*p>='A'&&*p<='Z'||*p>='a'&&*p<='z') letter++;
p++;
}
printf("letter=%d\n",letter);
}
2.輸入一個(gè)字符串,判斷這個(gè)字符串是否為“回文”。
#include "stdio.h"
#include "string.h"
main()
{
char *str;
int a,b,flag=0;
printf("please input string:");
gets(str);
a=strlen(str);
for(b=0;b<=a/2;b++)
if(*(str+b)==*(str+a-b-1)) flag=1;
else break;
if(flag) printf("yes!\n");
else printf("no!\n");
}
3.寫一個(gè)函數(shù),求字符串長度。在在主函數(shù)中輸入字符串并輸出長度。
#include "stdio.h"
int length(char *p)
{
int n=0;
while(*p++) n++;
return n;
}
main()
{
char *p;
gets(p);
printf("length=%d\n",length(p));
}
4.有一字符串包含N個(gè)字符,寫一個(gè)函數(shù), 將字符串中從第M個(gè)字符開始的全部字符復(fù)制成為另一個(gè)字符 串。
#include "stdio.h"
void scopy(char *psn,char *psm,int m)/*自定義函數(shù)*/
{
while(*(psn+m)) /*第m位置開始*/
*psm++=*(m+psn++);/*復(fù)制字符*/
*psm='\0'; /*加結(jié)束標(biāo)志*/
}
main()
{
char sn[100]="abcdefghijklmnopqrstuvwxyz",sm[100];
int m,n;
n=strlen(sn); /*求sn實(shí)際長度*/
printf("Please input m:");
scanf("%d",&m); /*輸入位置*/
if(m>n) printf("Position error!\n");
else
{
scopy(sn,sm,m); /*調(diào)用函數(shù),進(jìn)行復(fù)制*/
printf("\nThe new string is %s \n",sm);/*結(jié)果輸出*/
}
}