字符串—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.可以无限读取, ...
随机推荐
- nginx、php-fpm、mysql用户权限解析
nginx本身不能处理PHP,它只是个web服务器.当接收到客户端请求后,如果是php请求,则转发给php解释器处理,并把结果返回给客户端.如果是静态页面的话,nginx自身处理,然后把结果返回给客户 ...
- css样式—字体垂直、水平居中
“来,老板娘,给个div瞅瞅”: “好的,宇哥,来了了了”: <div class="tt">啦啦啦</div> “给各样啊,我去”: “是”: .tt{ ...
- Codeforces Round #371 (Div. 2) - A
题目链接:http://codeforces.com/contest/714/problem/A 题意:有两个人A,B 给定A的时间区间[L1,R1], B的时间区间[L2,R2],然后在正好K分钟的 ...
- DSP using MATLAB 示例Example3.23
代码: % Discrete-time Signal x1(n) : Ts = 0.0002 Ts = 0.0002; n = -25:1:25; nTs = n*Ts; x1 = exp(-1000 ...
- 如何查看经过编码的cookie?
方法1.去在线工具网站(http://tool.oschina.net/encode?type=2)手动复制编码的cookie,转码后查看. 方法2.用火狐浏览器打开网页,如果有历史记录(存在cook ...
- idea 开发环境jdk崩溃
## A fatal error has been detected by the Java Runtime Environment:## EXCEPTION_ACCESS_VIOLATION (0x ...
- applet示例 WelcomeApplet.java <Core Java>
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Font; import java.awt.Grap ...
- js中for in 和 for each in的用法和区别
区别一: for in是javascript 1.0 中发布的. for each in是作为E4X标准的一部分在javascript 1.6中发布的,而它不是EC ...
- HDU3395 Special Fish(最大费用任意流)
题目要的并不是最大匹配下得到的最大的结果. 网上流行的做法是加边.其实,在连续增广的时候求得一个可行流的总费用为负就停止这样就行了. #include<cstdio> #include&l ...
- ural 2067. Friends and Berries
2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...