C语言程序设计实验报告——实验八
2022/1/1 20:12:38
本文主要是介绍C语言程序设计实验报告——实验八,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C语言程序设计实验报告——实验八
- 实验八 指针
- 一、实验目的及要求
- 二、实验环境
- 三、实验内容
- 实验题目(1)
- 实验题目(2)
- 实验题目(3)
- 实验题目(4)
实验八 指针
一、实验目的及要求
1、熟练掌握指针变量的定义和应用,指向数组、字符串、函数的指针的定义和应用。 2、掌握指针数组定义和应用,指针的指针的定义和应用,返回指针值的函数的定义和应用。
二、实验环境
硬件要求:计算机一台。 软件要求:Windows操作系统,Dev-C++或VC++6.0编译环境
三、实验内容
实验题目(1)
输入3个字符串,按由小到大的顺序输出。
#include<stdio.h> #include<string.h> void sort(char *s1,char *s2){ char temp[20]; if(strcmp(s1,s2)>0){ strcpy(temp,s1); strcpy(s1,s2); strcpy(s2,temp); } } int main(){ char str1[20],str2[20],str3[20]; char *p1=str1,*p2=str2,*p3=str3; printf("请输入三个字符串:\n"); gets(p1); gets(p2); gets(p3); sort(p1,p2); sort(p1,p3); sort(p2,p3); printf("按照由小到大的顺序输出如下:\n%s\n%s\n%s\n",p1,p2,p3); return 0; }
实验题目(2)
写一个函数void my_strcpy(char *to, char *from),实现两个字符串的复制。
#include <stdio.h> void my_strcpy(char *from, char *to) { for(;*from!='\0';from++,to++) { *to=*from; } *to='\0'; } int main() { char *a="I love C!"; char b[]="Hello world!"; char *p=b; printf("string a=%s\nstring b=%s\n",a,b); printf("\ncopy string a to string b:\n"); my_strcpy(a,p); printf("string a=%s\nstring b=%s\n",a,b); return 0; }
实验题目(3)
用函数实现将n个数按照输入顺序的逆序排列。
#include<stdio.h> #include<math.h> void sort (int *p,int m) // 将n个数逆序排列函数 { int i; int temp, *p1,*p2; for (i=0;i<m/2;i++) { p1=p+i; p2=p+(m-1-i); temp=*p1; *p1=*p2; *p2=temp; } } int main() { int i,n; int *p; int num[20]; printf("input n:"); scanf("%d",&n); printf("please input these numbers:\n"); for (i=0;i<n;i++) scanf("%d",&num[i]); p=&num[0]; sort(p,n); printf("Now,the sequence is:\n"); for (i=0;i<n;i++) printf("%d ",num[i]); printf("\n"); return 0; }
实验题目(4)
输入一行文字,找出其中大写字母、小写字母、空格、数字和其它字符各有多少。
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 100 int main() { int a=0,b=0,c=0,d=0,e=0; char s[N]; char *p; p=s; printf("Please input any words:\n"); gets(s); while(*p!='\0') { if(*p>='A'&& *p<='Z') a++; else if(*p>='a'&& *p<='z') b++; else if(*p==' ') c++; else if(*p>='0'&& *p<='9') d++; else e++; p++; } printf("大写字母: %d\n",a); printf("小写字母: %d\n",b); printf("空格: %d\n",c); printf("数字: %d\n",d); printf("其他字符: %d\n",e); return 0; }
这篇关于C语言程序设计实验报告——实验八的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-21拼接的xml报文,尖括号都被转移成了< 是什么原因-icode9专业技术文章分享
- 2024-09-21Svg Sprite Icon教程:从入门到实践
- 2024-09-21Svg Sprite Icon实战:从入门到上手
- 2024-09-20构建一个多PDF RAG聊天机器人:使用Langchain和Streamlit及代码
- 2024-09-20whatsapp webhook 回调的签名验证偶尔会失败是什么原因-icode9专业技术文章分享
- 2024-09-19Excel数据导出课程:初学者必备教程
- 2024-09-19Excel数据导入课程:新手入门指南
- 2024-09-19RBAC的权限管理入门教程
- 2024-09-19如何使用Svg Sprite Icon制作图标
- 2024-09-19uniapp 如何实现点赞后全局更新数据-icode9专业技术文章分享