笔记:编写字符串拷贝函数

浪尽此生 提交于 2020-02-02 05:55:17

参考书目:C/C++规范设计简明教程(第2版)P203


//例8.1 字符串拷贝
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

/*  文件名:void myStrCpy(char pDes, char pSrc)
	作用:将pSrc字符串拷贝为pDes
	输入参数:参数1,源字符串;参数2 目标字符串
	输出参数:无
	使用方法: myStrCpy(pDes, pSrc);

*/
void myStrCpy(char pDes[], char pSrc[]);	//输入参数为数组

/*  文件名:void myStrCpy1(char pDes, char pSrc)
	作用:将pSrc字符串拷贝为pDes
	输入参数:参数1,源字符串;参数2 目标字符串
	输出参数:无
	使用方法: myStrCpy(pDes, pSrc);

*/
void myStrCpy1(char *pDes, char *pSrc);		//输入参数为指针

int main()
{
	cout << "Hello World!\n";
	char des[20], src[20], des1[20];
	cin >> src;
	myStrCpy(des, src);
	cout << des << endl;		//拷贝后的字符串,使用数组
	myStrCpy1(des1, src);
	cout << des1 << endl;		//拷贝后的字符串,使用指针

	getchar();
}

void myStrCpy(char pDes[], char pSrc[])	//输入参数为数组
{
	int i = 0;
	while (pSrc[i] != 0)		//未到字符串末尾
	{
		pDes[i] = pSrc[i];
		i++;
	}
	pDes[i] = 0;
}

void myStrCpy1(char *pDes, char *pSrc)		//输入参数为指针
{
	int i = 0;
	while (*(pSrc+i) != 0)		//未到字符串末尾
	{
		*(pDes+i )= *(pSrc + i);
		i++;
	}
	*(pDes + i) = 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!