字符串—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.可以无限读取, ...
随机推荐
- ThinkPHP3.2 volist嵌套循环显示原理
php页面:$fatherList = $Document->where('pid=1')->select(); foreach($fatherList as $n=> ...
- jsp include指令
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- JSON语法简介 介绍 json
JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation),类似 XML,但比 XML 更小.更快,更易解析. 实例 { "employees ...
- 20145223《Java程序程序设计》第9周学习总结
20145223<Java程序设计>第9周学习总结 教材学习内容总结 第十六章:整合数据库 JDBC入门 1.JDBC简介: 2.JDBC主要分成两个部分,JDBC应用程序开发者接口和JD ...
- wpf,ListBox,ScrollViewer内容向左向右偏移指定位置
public partial class Example : UserControl { private ScrollViewer myScrollViewer; public Example() { ...
- Spring mvc 验证码的做法
http://jingyan.baidu.com/article/4f7d5712da7a131a201927b0.html
- 并查集(加权) LA 4487 Exclusive-OR
题目传送门 题意:训练指南P245 分析:首先这道是经典的并查集题目,利用异或的性质.异或性质:x ^ 0 = x -> a ^ a = 0 -> x ^ a ^ a = x,即一个数对某 ...
- MFC CPen CBrush CFont
在OnDraw函数内定义后使用 使用时,要pDC->SelectObject(),如pDC->SelectObject(font); CPen://draw line Pen.Create ...
- wordpress发送测试邮件
下面的邮箱设置使用了qq邮箱的设置 写上接收测试邮件的邮箱 再send test
- Dependency Properties
Introduction Value resolution strategy The magic behind it How to create a DepdencyProperty Readon ...