ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C语言程序设计实验报告——实验八

2022-01-01 19:00:13  阅读:136  来源: 互联网

标签:p2 p1 int C语言 char include 实验 程序设计 实验报告


C语言程序设计实验报告——实验八

实验八 指针

一、实验目的及要求

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;
}

标签:p2,p1,int,C语言,char,include,实验,程序设计,实验报告
来源: https://blog.csdn.net/qq_45763375/article/details/113773855

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有