C语言指针精选习题和答案(精心整理)_第1页
C语言指针精选习题和答案(精心整理)_第2页
C语言指针精选习题和答案(精心整理)_第3页
C语言指针精选习题和答案(精心整理)_第4页
C语言指针精选习题和答案(精心整理)_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、用指针方法编写一个程序,输入 3 个整数,将它们按由小到大的顺序输出#include <stdio.h>void swap(int *pa,int *pb)int temp;temp = *pa;*pa = *pb;*pb = temp;void main()int a,b,c,temp;scanf("%d%d%d",&a,&b,&c);if(a>b)swap(&a,&b);if(b>c)swap(&b,&c);if(a>c)swap(&a,&c);printf(&qu

2、ot;%d,%d,%d",a,b,c);二、C语言 用指针方法 输入3个字符串 按由小到大顺序输出#include "stdio.h"#include "string.h"int main(int argc, char* argv)char *t;char *p1=NULL,*p2=NULL,*p3=NULL;char ch120=0,ch220=0,ch320=0;p1=ch1;p2=ch2;p3=ch3;printf("No1:");scanf("%s",p1);fflush(stdin);print

3、f("No2:");scanf("%s",p2);fflush(stdin);printf("No3:");scanf("%s",p3);fflush(stdin);if(strcmp(p1,p2)>0)t=p1;p1=p2;p2=t;if(strcmp(p1,p3)>0)t=p1;p1=p3;p3=t;if(strcmp(p2,p3)>0)t=p2;p2=p3;p3=t;printf("%sn%sn%sn",p1,p2,p3);return 0;9.4 编程输入一行文字,找出

4、其中的大写字母,小写字母,空格,数字,及其他字符的个数#include<stdio.h>void main()int a=0,b=0,c=0,d=0,e=0,i=0;char *p,s20;while(si=getchar()!='n')i+;p=s;while(*p!=10)if(*p>='A'&&*p<='Z')a+;else if(*p>='a'&&*p<='z')b+;else if(*p=' ')c+;else if(*p

5、>='0'&&*p<='9')d+;else e+;p+;printf(" 大写字母 %d 小写字母 %dn",a,b); printf(" 空格 %d 数字 %d 非字符 %dn",c,d,e);9.5 写一个函数,将3 3矩阵转置#include "stdio.h"void Transpose(int (*matrix)3)int temp;int i, j;for(i=1;i<3;i+)/*转置*/for(j=0;j<i;j+)temp = *(*(matr

6、ix+j)+i);*(*(matrix+j)+i) = *(*(matrix+i)+j);*(*(matrix+i)+j) = temp; void main()int a33 = 1,2,3,4,5,6,7,8,9;Transpose(a);for(int i = 0;i<3;i+)for(int j=0;j<3;j+)printf("%d ",aij);printf("n");9.6 用指向一维数组的指针做函数参数#include<stdio.h>#include<string.h>int main()13for(

7、i=0;i<10;i+) printf("%s/n",stri);return 0;void sort(char (*s)6)sint i,j;char temp6, *t;t=temp;for(i=0;i<9;i+)/i法则,for(j=0;j<9-i;j+)次;依次类推;void sort(char (*s)6);/一维数组的指针做函数参数int i;char str106;char (*p)6;/定义一维数组的指针做函数参数printf("please input string:/n");for(i=0;i<10;i+)sc

8、anf("%s",&stri);p=str;/ 将str 一维数组指针,赋值给 p;sort(p);printf("the output sequence :/n");指向一维数组的指针做函数参数;应该小于9;如果小于10,那么就比较了 9+1次;按照冒泡第一次比较需要9次就是i=0到i=8共九次;第二次需要比较8if(strcmp(sj,sj+1)>0)strcpy(t,sj);strcpy(sj,sj+1);strcpy(sj+1,t);9.7 编一程序,用指针数组在主函数中输入十个等长的字符用。用另一函数对它们排序,然后在主函数中输出

9、10个已排好序的字符串/用指针数组处理#include<stdio.h>#include<string.h>int main()void sort(char *);int i;char str106, *p10;printf("please input 10 string:/n");for(i=0;i<10;i+)/首先将10个str的首地址赋值给 10个pi;pi=str叱将第i个字符串的首地址赋予指针数组p的第i个元素;for(i=0;i<10;i+)scanf("%s",pi);/scanf 输入到 &p

10、isort(p);printf("the output 10 string:/n");for(i=0;i<10;i+)printf("%s/n",pi);/ 输出到 pi;void sort(char *s口)char *temp;int i,j;for(i=0;i<9;i+)for(j=0;j<9-i;j+)if(strcmp(*(s+j),*(s+j+1)>0)temp=*(s+j);/*(s+j)指向数组指针,我想应该是字符串的首地址;所以可以直接赋值给temp指针;*(s+j)=*(s+j+1);*(s+j+1)=temp

11、; 9.8 指针 将n个数按输入时顺序的逆序排列,用函数实现#include <stdio.h> void reverse(int a,int n) int *p;for(p=a+n-1;p>=a;p-)printf("%4d",*p);printf("n");main() int a20,n;int i;printf("Input the length of array:");scanf("%d",&n);printf("Input the number of array:&q

12、uot;);for(i=0;i<n;i+)scanf("%d",&ai);reverse(a,n);9.9 写一函数,实现两个字符串的比较。即自己写一个strcmp 函数,函数原型为:int stremp(char *p1,char *p2)设pl指向字符串Si, p2指向字符串s2。要求:当s1=s2时,返回值为0。当si不等于s2 时,返回它们二者的第一个不同字符的ASCII码差值(如“ BOY与"BAD1,第二字母不同,"O'与"A”之差为79-65=14);如果s1>s2,则输出正值;如果 s1<s2,

13、则输出负值。#include <stdio.h>main() int strcmp(char *pi,char *p2);int m;char stri20,str220,*pi,*p2;printf("Input two strings:n");scanf("%s",stri);scanf("%s",str2);pi=&stri0;p2=&str20;m=strcmp(pi,p2);printf("result: %dn",m);/*两个字符串比较的函数*/*相等时返回结果0*/不等时

14、返回结果为第一个不等字符int strcmp(char *p1,char *p2) int i;i=0;while(*(p1+i)=*(p2+i)if(*(p1+i+)='0') return(0);return(*(p1+i)-*(p2+i);/*ASCII码的差值*/运行情况如下: Input two strings:CHINA /Chen /Result: -32 Input two strings:hello!/Hello!/Result: 0 Input two stings:dog /cat /result: 19.10 编一个程序,打入月份号,输出该月的英文月名。

15、例如,输入“3”,则输出“March”, 要求用指针数组处理。#include <stdio.h>main() char *month_name13="illegal month","January","February","March","April","May","June","July","August","September","October","

16、;November","D ecember"int n;printf("Input month:");scanf("%d",&n);if(n<=12)&&(n>=1)printf("It is %s.n”,*(month_name+n);elseprintf("It is wrong.n");运行结果: Input month: 2It is February. Input month: 8It is August.3) Input month: 13 乙It

17、 is wrong.9.11 c语言:将字符串computer赋给一个字符数组,然后从第一个字母开始间隔地输出该串。请用指针实现#include <stdio.h>#include <string.h>#define MAX_LENGTH 32int main()char strMAX_LENGTH = 0;char *pStr = (char*)&str;/1. 将字符串 computer 赋给一个字符数组strcpy(str, "computer");/2. 然后从第一个字母开始间隔地输出该串while(*pStr != '0&#

18、39; )printf("%cn", *pStr);pStr+;return 1;9.12 从键盘上输入一个字符串,按后按照下面要求输出一个新字符串, 新的字符串是在原来字符串中, 每两个字符之间插入一个空格, 如原来的字符串为“ abcd” , 新产生的字符串应为“ a b c d ”编写一个程序咯用 C 就是输出的字符是 每两个字符之间有一个空格#include<stdio.h>void main()char a10,b10,i=0,j=0;printf(" 输出字符串 ");scanf("%s",a);/ abcde

19、ffor(i=0;ai!='0'i+)bj+=ai;bj+=' 'bj='0'printf("%s",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 。 )#include &l

20、t;iostream.h>void method(int n,int m,int *a)int *p=a,*q=new intm;p=p+n-1;for(int i=0;i<m;i+)qi=*p;p+;p=p-m;for(i=0;i<m;i+)*p=qm-1-i;p+;int main()int a10=2,4,6,8,10,12,14,16,18,20;method(4,5,a);for(int i=0;i<10;i+)cout<<ai<<' 'return 0;9.3有一字符串,包含n个字符。写一个函数,将此字符串从第m个字符

21、开始的全部字符复制成为另一个字符串并输出void strcpyn(char *s,char *t, int n)char *p=s+n;char *q=t;while(*p)*q=*p;q+;p+;*q='0'main()char s100=0;char t100=0;int n=0;printf("input string s:n");scanf("%s",s);printf("input start n:n");scanf("%d",&n);strcpyn(s,t,n);puts(t);

22、-实验 101. 设计函数char *insert(s1,s2,n), 用指针实现在字符串 s1 中的指定位置n 处插入字符串s2#include <stdio.h>char* insert(char* s1,char* s2,int n)int j=0;char* ss=new char100;char* tsptr=ss;for(int i=0;i<n;i+)*ss+=*s1+;while(*s2!='0')*ss+=*s2+;while(*s1!='0')*ss+=*s1+;*ss='0'return tsptr;void main()char s1="123456789"char s2="1234"char* ss=insert(s1,s2,4);printf("%s",ss);2. 利用

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论