C语言指针精选习题集和答案解析Word下载.docx

上传人:b****4 文档编号:7360389 上传时间:2023-05-08 格式:DOCX 页数:17 大小:18.48KB
下载 相关 举报
C语言指针精选习题集和答案解析Word下载.docx_第1页
第1页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第2页
第2页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第3页
第3页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第4页
第4页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第5页
第5页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第6页
第6页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第7页
第7页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第8页
第8页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第9页
第9页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第10页
第10页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第11页
第11页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第12页
第12页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第13页
第13页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第14页
第14页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第15页
第15页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第16页
第16页 / 共17页
C语言指针精选习题集和答案解析Word下载.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C语言指针精选习题集和答案解析Word下载.docx

《C语言指针精选习题集和答案解析Word下载.docx》由会员分享,可在线阅读,更多相关《C语言指针精选习题集和答案解析Word下载.docx(17页珍藏版)》请在冰点文库上搜索。

C语言指针精选习题集和答案解析Word下载.docx

printf("

No1:

"

);

scanf("

%s"

p1);

fflush(stdin);

No2:

scanf("

p2);

No3:

p3);

if(strcmp(p1,p2)>

0)

{t=p1;

p1=p2;

p2=t;

if(strcmp(p1,p3)>

p1=p3;

p3=t;

if(strcmp(p2,p3)>

{t=p2;

p2=p3;

%s\n%s\n%s\n"

p1,p2,p3);

return0;

9.4编程输入一行文字,找出其中的大写字母,小写字母,空格,数字,及其他字符的个数

#include<

inta=0,b=0,c=0,d=0,e=0,i=0;

char*p,s[20];

while((s[i]=getchar())!

='

\n'

)i++;

p=s;

while(*p!

=10)

if(*p>

A'

&

*p<

Z'

a++;

elseif(*p>

a'

z'

b++;

elseif(*p=='

'

c++;

0'

9'

d++;

elsee++;

p++;

大写字母%d小写字母%d\n"

a,b);

空格%d数字%d非字符%d\n"

c,d,e);

9.5写一个函数,将33矩阵转置

voidTranspose(int(*matrix)[3])

inttemp;

inti,j;

for(i=1;

i<

3;

i++)/*转置*/

{

for(j=0;

j<

i;

j++)

temp=*(*(matrix+j)+i);

*(*(matrix+j)+i)=*(*(matrix+i)+j);

*(*(matrix+i)+j)=temp;

}

voidmain()

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

Transpose(a);

for(inti=0;

i++)

for(intj=0;

%d"

a[i][j]);

\n"

9.6用指向一维数组的指针做函数参数

string.h>

intmain()

 

voidsort(char(*s)[6]);

//一维数组的指针做函数参数

inti;

charstr[10][6];

char(*p)[6];

//定义一维数组的指针做函数参数

pleaseinputstring:

/n"

for(i=0;

10;

str[i]);

p=str;

//将str一维数组指针,赋值给p;

sort(p);

theoutputsequence:

%s/n"

str[i]);

return0;

voidsort(char(*s)[6])//s指向一维数组的指针做函数参数;

inti,j;

chartemp[6],*t;

t=temp;

for(i=0;

9;

i++)//i应该小于9;

如果小于10,那么就比较了9+1次;

按照冒泡法则,

for(j=0;

9-i;

j++)//第一次比较需要9次就是i=0到i=8共九次;

第二次需要比较8次;

依次类推;

if(strcmp(s[j],s[j+1])>

strcpy(t,s[j]);

strcpy(s[j],s[j+1]);

strcpy(s[j+1],t);

9.7编一程序,用指针数组在主函数中输入十个等长的字符串。

用另一函数对它们排序,然后在主函数中输出10个已排好序的字符串

//用指针数组处理

voidsort(char*[]);

charstr[10][6],*p[10];

pleaseinput10string:

i++)//首先将10个str的首地址赋值给10个p[i];

p[i]=str[i];

//将第i个字符串的首地址赋予指针数组p的第i个元素;

p[i]);

//scanf输入到&

p[i]

sort(p);

theoutput10string:

//输出到p[i];

voidsort(char*s[])

char*temp;

if(strcmp(*(s+j),*(s+j+1))>

temp=*(s+j);

//*(s+j)指向数组指针,我想应该是字符串的首地址;

所以可以直接赋值给temp指针;

*(s+j)=*(s+j+1);

*(s+j+1)=temp;

9.8指针将n个数按输入时顺序的逆序排列,用函数实现

voidreverse(inta[],intn)

{int*p;

for(p=a+n-1;

p>

=a;

p--)

%4d"

*p);

main()

{inta[20],n;

inti;

Inputthelengthofarray:

%d"

n);

Inputthenumberofarray:

n;

a[i]);

reverse(a,n);

9.9写一函数,实现两个字符串的比较。

即自己写一个strcmp函数,函数原型为:

intstremp(char*p1,char*p2)

设p1指向字符串s1,p2指向字符串s2。

要求:

当s1=s2时,返回值为0。

当s1不等于s2时,返回它们二者的第一个不同字符的ASCII码差值(如“BOY”与“BAD”,第二字母不同,“O”与“A”之差为79-65=14);

如果s1>

s2,则输出正值;

如果s1<

s2,则输出负值。

{intstrcmp(char*p1,char*p2);

intm;

charstr1[20],str2[20],*p1,*p2;

Inputtwostrings:

str1);

str2);

p1=&

str1[0];

p2=&

str2[0];

m=strcmp(p1,p2);

result:

%d\n"

m);

intstrcmp(char*p1,char*p2)/*两个字符串比较的函数*/

{inti;

i=0;

while(*(p1+i)==*(p2+i))

if(*(p1+i++)=='

\0'

)return(0);

/*相等时返回结果0*/

return(*(p1+i)-*(p2+i));

/*不等时返回结果为第一个不等字符ASCII码的差值*/

运行情况如下:

①Inputtwostrings:

CHINA↙

Chen↙

Result:

-32

②Inputtwostrings:

hello!

Hello!

0

③Inputtwostings:

dog↙

cat↙

result:

1

9.10编一个程序,打入月份号,输出该月的英文月名。

例如,输入“3”,则输出“March”,要求用指针数组处理。

{char*month_name[13]={"

illegalmonth"

"

January"

February"

March"

April"

"

May"

June"

July"

August"

September"

October"

November"

December"

};

intn;

Inputmonth:

if((n<

=12)&

(n>

=1))

Itis%s.\n"

*(month_name+n));

else

Itiswrong.\n"

运行结果:

①Inputmonth:

2↙

ItisFebruary.

②Inputmonth:

8↙

ItisAugust.

③Inputmonth:

13↙

Itiswrong.

9.11c语言:

将字符串computer赋给一个字符数组,然后从第一个字母开始间隔地输出该串。

请用指针实现

#defineMAX_LENGTH32

charstr[MAX_LENGTH]={0};

char*pStr=(char*)&

str;

//1.将字符串computer赋给一个字符数组

strcpy(str,"

computer"

//2.然后从第一个字母开始间隔地输出该串

while(*pStr!

='

%c\n"

*pStr);

pStr++;

return1;

9.12从键盘上输入一个字符串,按后按照下面要求输出一个新字符串,新的字符串是在原来字符串中,每两个字符之间插入一个空格,如原来的字符串为“abcd”,新产生的字符串应为“abcd”

编写一个程序咯用C就是输出的字符是每两个字符之间有一个空格

chara[10],b[10],i=0,j=0;

输出字符串"

a);

//abcdef

a[i]!

;

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

b[j++]='

b[j]='

b);

9.13设有一数列,包含10个数,已按升序排好。

现要求编一程序,它能够把从指定位置开始的n个数按逆序重新排列并输出新的完整数列。

进行逆序处理时要求使用指针方法(例如:

原数列为2,4,6,8,10,12,14,16,18,20,若要求把从第4个数开始的5个数按逆序重新排列,则得到新数列为2,4,6,16,14,12,10,8,18,20。

iostream.h>

voidmethod(intn,intm,int*a)

int*p=a,*q=newint[m];

p=p+n-1;

for(inti=0;

m;

q[i]=*p;

p++;

p=p-m;

*p=q[m-1-i];

inta[10]={2,4,6,8,10,12,14,16,18,20};

method(4,5,a);

cout<

<

a[i]<

'

9.3有一字符串,包含n个字符。

写一个函数,将此字符串从第m个字符开始的全部字符复制成为另一个字符串并输出

voidstrcpyn(char*s,char*t,intn)

char*p=s+n;

char*q=t;

while(*p)

*q=*p;

q++;

*q='

chars[100]={0};

chart[100]={0};

intn=0;

inputstrings:

s);

inputstartn:

strcpyn(s,t,n);

puts(t);

-

实验10

1.设计函数char*insert(s1,s2,n),用指针实现在字符串s1中的指定位置n处插入字符串s2

#include<

char*insert(char*s1,char*s2,intn)

intj=0;

char*ss=newchar[100];

char*tsptr=ss;

*ss++=*s1++;

while(*s2!

*ss++=*s2++;

while(*s1!

*ss='

returntsptr;

chars1[]="

123456789"

chars2[]="

1234"

char*ss=insert(s1,s2,4);

ss);

2.利用指针完成字符串复制函数char*strcpy(char*s1,char*s2)

char*fun(char*s1,char*s2)

{char*p=s2;

while((*s1)!

*s2=*s1;

s1++,s2++;

returnp;

voidmain(void)

char*p,*strcpy;

char*s1="

worldcup!

char*s2="

goodluck!

s1=%s\ns2=%s\n"

s1,s2);

p=fun(s1,s2);

s1,s2)

实验9

1.编写一个使用指针的c函数,交换数组a和数组b中的对应元素

stdlib.h>

voidex(int*a,int*b,intn){

inti,tmp;

for(i=0;

i++){

tmp=a[i];

a[i]=b[i];

b[i]=tmp;

intx[5]={1,2,3,4,5};

inty[5]={6,7,8,9,10};

inti,n=5;

ex(x,y,n);

\nX:

i++)printf("

x[i]);

\nY:

y[i]);

system("

pause"

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2