字符串—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.可以无限读取, ...
随机推荐
- windows主机开启openssl的方法
转自:http://www.feichang56.com/openssl/
- DSP using MATLAB 示例Example3.8
代码: x = rand(1,11); n = 0:10; k = 0:500; w = (pi/500)*k; % [0,pi] axis divided into 501 points. X = ...
- UVALive6900 Road Repair(树的点分治)
题目大概说一棵树,树边有费用和收益两个属性,求一条收益和最大的路径满足费用和不超过C. 树上任意两点的路径都可以看成是过某一个子树根的路径,显然树分治. 治的时候要解决的一个问题是,找到费用小于等于某 ...
- HDU4511 小明系列故事——女友的考验(AC自动机 + DP)
题目大概说有平面有n个点,从1点出发走到n点,每一步只能走到序号比当前更大的点且走的序列不能包含给定的m个序列中的任何一个,问1走到n的最短路. 用m个序列建个AC自动机,后缀包含整个序列的结点标记一 ...
- SQL Prompt
SQL Prompt介绍编辑 SQL Prompt[1] 是一款拥有SQL智能提示功能的SQL Server和VS插件.SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动进行检索 ...
- Fetch from Upstream 变灰失效
Team——>Remote——>Configure Fetch from Upstream… Team——>Remote——>Configure Push to Upstre ...
- javascript拾掇
用javascript如何给span赋值呢?一般有两种方法: 1>输出html <body onload="s()"><span id="hell ...
- jq prepend() 方法在被选元素的开头(仍位于内部)插入指定内容。 提示:prepend() 和 prependTo() 方法作用相同。差异在于语法:内容和选择器的位置,以及 prependTo() 无法使用函数来插入内容。
<html><head><script type="text/javascript" src="/jquery/jquery.js" ...
- OpenResy+Lua 利用百度识图 将图片地址解析成文字
LUA代码:(注:LUA里有一个调用百度识图的接口IP:123.125.115.189(stu.baidu.com),不知为什么我的虚拟机无法解析stu.baidu.com,所以我只能PING出IP来 ...
- topcoder SRM 625 DIV2 AddMultiply
由于题目告诉肯定至少存在一种解, 故只需要根据条件遍历一下, vector <int> makeExpression(int y) { vector<int> res; ; i ...