c语言指针精选习题和答案

上传人:第*** 文档编号:30578887 上传时间:2018-01-30 格式:DOC 页数:13 大小:84.50KB
返回 下载 相关 举报
c语言指针精选习题和答案_第1页
第1页 / 共13页
c语言指针精选习题和答案_第2页
第2页 / 共13页
c语言指针精选习题和答案_第3页
第3页 / 共13页
c语言指针精选习题和答案_第4页
第4页 / 共13页
c语言指针精选习题和答案_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《c语言指针精选习题和答案》由会员分享,可在线阅读,更多相关《c语言指针精选习题和答案(13页珍藏版)》请在金锄头文库上搜索。

1、一、用指针方法编写一个程序,输入 3 个整数,将它们按由小到大的顺序输出#include 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, if(ab) swap( if(bc) swap( if(ac) swap( printf(%d,%d,%d,a,b,c); 二、C 语言 用指针方法 输入 3 个字符串 按由小到大顺序输出#include stdio.h#include string.hint main(int

2、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);printf(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(s

3、trcmp(p2,p3)0)t=p2;p2=p3;p3=t;printf(%sn%sn%sn,p1,p2,p3);return 0; 9.4 编程输入一行文字,找出其中的大写字母,小写字母,空格,数字,及其他字符的个数#include 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=a&*p=0&*p#includeint main() void sort(char (*s)6);/一维数组的指针做函数参数int i;cha

4、r str106;char (*p)6;/定义一维数组的指针做函数参数printf(please input string:/n);for(i=0;i0)strcpy(t,sj);strcpy(sj,sj+1);strcpy(sj+1,t);9.7 编一程序,用指针数组在主函数中输入十个等长的字符串。用另一函数对它们排序,然后在主函数中输出 10 个已排好序的字符串/用指针数组处理#include#includeint main()void sort(char *);int i;char str106, *p10;printf(please input 10 string:/n);for(i=

5、0;i0)temp=*(s+j);/*(s+j)指向数组指针,我想应该是字符串的首地址;所以可以直接赋值给temp 指针;*(s+j)=*(s+j+1);*(s+j+1)=temp;9.8 指针 将 n 个数按输入时顺序的逆序排列,用函数实现#include 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,printf(Input the number of

6、 array:);for(i=0;is2,则输出正值;如果 s1main() int strcmp(char *p1,char *p2);int m;char str120,str220,*p1,*p2;printf(Input two strings:n);scanf(%s,str1);scanf(%s,str2);p1=p2=printf(result: %dn,m);int strcmp(char *p1,char *p2) /*两个字符串比较的函数*/ int i;i=0;while(*(p1+i)=*(p2+i)if(*(p1+i+)=0) return(0); /*相等时返回结果

7、0*/return(*(p1+i)-*(p2+i); /*不等时返回结果为第一个不等字符ASCII 码的差值*/运行情况如下: Input two strings:CHINAChenResult: -32 Input two strings:hello! Hello! Result: 0 Input two stings: dogcatresult: 19.10 编一个程序,打入月份号,输出该月的英文月名。例如,输入“3”,则输出“March”,要求用指针数组处理。#include main() char *month_name13=illegal month,January,February

8、,March,April,May,June,July,August,September,October,November,December;int n;printf(Input month: );scanf(%d,if(n=1)printf(It is %s.n,*(month_name+n);elseprintf(It is wrong.n);运行结果: Input month: 2It is February. Input month: 8 It is August. Input month: 13It is wrong.9.11c 语言: 将字符串 computer 赋给一个字符数组,然

9、后从第一个字母开始间隔地输出该串。请用指针实现#include #include #define MAX_LENGTH 32int main()char strMAX_LENGTH = 0;char *pStr = (char*)/1. 将字符串 computer 赋给一个字符数组strcpy(str, computer);/2. 然后从第一个字母开始间隔地输出该串while(*pStr != 0 )printf(%cn, *pStr);pStr+;return 1;9.12 从键盘上输入一个字符串,按后按照下面要求输出一个新字符串, 新的字符串是在原来字符串中, 每两个字符之间插入一个空格,

10、 如原来的字符串为“abcd”, 新产生的字符串应为“a b c d”编写一个程序咯 用 C 就是输出的字符 是 每两个字符之间有一个空格#includevoid main()char a10,b10,i=0,j=0;printf(输出字符串);scanf(%s,a);/ abcdeffor(i=0;ai!=0;i+)bj+=ai;bj+= ;bj=0;printf(%s,b);9.13 设有一数列,包含 10 个数,已按升序排好。现要求编一程序,它能够把从指定位置开始的 n 个数按逆序重新排列并输出新的完整数列。进行逆序处理时要求使用指针方法(例如:原数列为 2,4,6,8,10,12,14

11、,16,18,20,若要求把从第 4 个数开始的 5 个数按逆序重新排列,则得到新数列为 2,4,6,16,14,12,10,8,18,20。 )#include void method(int n,int m,int *a)int *p=a,*q=new intm;p=p+n-1;for(int i=0;ichar* insert(char* s1,char* s2,int n)int j=0;char* ss=new char100;char* tsptr=ss; for(int i=0;i#includevoid ex(int *a, int *b, int n)int i,tmp;for (i=0;in;i+)tmp=ai;ai=bi;bi=tmp;void main()int x5=1,2,3,4,5;int y5=6,7,8,9,10;int i,n=5;ex(x,y,n);printf(n X: );for (i=0;in;i+)printf(%d ,xi);printf(n Y: );for (i=0;in;i+)printf(%d ,yi);system(pause);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 外语文库 > 英语学习

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号