字符串—strcpy
典型实现
//C语言标准库函数strcpy的一种典型的工业级的最简实现。 //返回值:目标串的地址。 //对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。 //参数:des为目标字符串,source为原字符串。 char* strcpy(char* des,const char* source)
{
char* r=des; assert((des != NULL) && (source != NULL)); while((*des++ = *source++)!='\0') ; //赋值表达式返回左操作数,所以在赋值NULL后,循环停止。
return r;
}
应用实例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
⒈strcpy的实现代码char * strcpy(char * strDest,const char * strSrc) {if ((NULL==strDest) || (NULL==strSrc)) //[1]throw "Invalid argument(s)"; //[2]char * strDestCopy = strDest; //[3]while ((*strDest++=*strSrc++)!='\0'); //[4]return strDestCopy;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
while( 1 ){ char temp; *strDestCopy = *strSrc; strDestCopy++; strSrc++; temp = *strSrc; if( '\0' == temp ) break;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
while ( *strSrc != '\0' ){ *strDestCopy = *strSrc; strDestCopy++; strSrc++;}*strDestCopy = *strSrc++; 也即:while ( *strSrc != '\0' ){ *strDestCopy++ = *strSrc++;}*strDestCopy=‘\0’; |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#include<iostream>#include<stdlib.h>using namespace std;char * strcpy( char * strDest, const char * strSrc ){ char * strDestCopy = strDest; if ((NULL==strDest)||(NULL==strSrc))throw "Invalid argument"; while ( (*strDest++=*strSrc++) != '\0' ); return strDestCopy;} void main( int argc, char * argv[] ){ char a[20], c[] = "i am teacher!"; try{ strcpy(a,c); } catch(char* strInfo) { cout << strInfo << endl; exit(-1); } cout << a << endl;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include<iostream>using namespace std;char *strcpy(char *strDes, const char *strSrc);//函数声明int main(){ const char *strSrc="helloworld"; char *strDes=NULL; strDes=strcpy(strDes,strSrc); cout<<"strSrc="<<strSrc<<endl; cout<<"strDes="<<strDes<<endl; if(strDes!=NULL) { free(strDes); strDes=NULL; } return 0;}char *strcpy(char *strDes, const char *strSrc){ assert(strSrc!=NULL); //若strSrc为NULL,则抛出异常。 strDes=(char *)malloc(strlen(strSrc)+1); //多一个空间用来存储字符串结束符'\0' char *p=strDes; while(*strSrc!='\0') { *p++=*strSrc++; } *p='\0'; return strDes;} |
|
1
2
3
4
5
6
7
8
9
|
char* p="how are you ?"; char name[20]="ABCDEFGHIJKLMNOPQRS"; strcpy(name,p); //name改变为"how are you ? "====>正确! strncpy(name,p, sizeof(name)); //name改变为"how are you ?" =====>正确!后续的字符将置为NULL |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
char* p="how are you ?"; char name[10]; strcpy(name,p); //目标串长度小于源串,错误! name[sizeof(name)-1]='\0'; //和上一步组合,弥补结果,但是这种做法并不可取,因为上一步出错处理方式并不确定 strncpy(name,p,sizeof(name)); //源串长度大于指定拷贝的长度sizeof(name),注意在这种情况下不会自动在目标串后面加'\0' name[sizeof(name)-1]='\0'; //和上一步组合,弥补结果 |
字符串—strcpy的更多相关文章
- 字符串strcpy
strcpy函数的表达方式: //把一个char组成的字符串循环右移n个,如:“abcdefghi",n=2,移动后"hiabcdefgh" #include <i ...
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...
- Python中字符串操作
#Python字符串操作 '''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' pri ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
- Python操作文件、文件夹、字符串
Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...
- python基础字符串操作
去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...
- OpenJudge计算概论-字符串排序
/*====================================================================== 字符串排序 总时间限制: 1000ms 内存限制: 6 ...
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...
- C语言 > 字符串和字符串函数
输入 gets() 函数 : 1.gets() 从标准输入设备读取字符串,以回车结束读取,使用'\0'结尾,回车符'\n'被舍弃没有遗留在缓冲区. 2.可以用来输入带空格的字符串. 3.可以无限读取, ...
随机推荐
- phpstudy配置ssl
https://yunpan.cn/cPEyzVycbkiE3 (提取码:03aa) 1.重写规则:http://www.cnphp.info/htaccess-rewrite.html 2.相关文档 ...
- loadrunner统计字符串中指定字符出现的次数
Action() { char *str="sdfas1,sdfsdf2,sdfsdfsdfdsf3,sdfsdfsdfsdfds4,fsdfdsf5,sdfdsfsd6,fsdfsd7sd ...
- 在Windows下快速搭建SVN服务器 VisualSVN
下载https://www.visualsvn.com/server/download/ 1.安装 安装SVN服务器: 安装的时候可以选择http协议还是https协议,http协议速度快一些,而ht ...
- 廖雪峰js教程笔记 1
遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...
- 关于初级java面试问题的一些整理 (部分转自他人)
1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部 ...
- 《DSP using MATLAB》示例Example4.5
代码: x1 = [1, 2, 3]; x2 = [2, 4, 3, 5]; % x1 x2 sequences % n1 = 0:1:2; n2 = 0:1:3; n1 = -1:1:1; n2 = ...
- Y86模拟器安装
Y86模拟器安装 这周需要学习Y86下的指令集开发,Y86和x86可以说是孪生兄弟,但是还是存在着一些小的差别.接下来介绍如何进行linux-debian平台下的Y86模拟器安装. 虚拟机VMware ...
- js兼容方法:事件添加|事件绑定|事件监听 addEvent
function addEvent(obj,sEvent,fn){ if(obj.attachEvent){ obj.attachEvent("on"+sEvent,fn); }e ...
- 获取datable中某行某列的数据
假设该DataTable有5行和两个字段“Name”,“Phone”, 我如何访问第3行的“Phone”字段的值. DataTable.Rows[2][1].ToString() DataTable. ...
- HTML5学习之路
出于公司项目需求,我现在开始学习html5,虽然零零散散有过一点,比如说新出的语义化标签,本地存储之类的,但是从来都没有系统的去了解.