|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 guoquanli 于 2020-6-10 16:22 编辑
- void self_strcpy(char *source, char *copyObj){//自己写的字符串拷贝函数
- char *tempSource = source;
- char *tempObj = copyObj;
- int sourceLen = 0; // 记录字符串的长度
- int copyObjLen = 0;
- printf("%c\n",*tempSource);
- printf("%c\n",*tempObj);
- while(*tempSource){ // 字符串source的长度
- sourceLen++;
- tempSource++;
- }
- while(*tempObj){//字符串copyObj的长度
- copyObjLen++;
- tempObj++;
- }
- printf("%d\n",*tempSource);
- printf("%d\n",*tempObj);
- printf("%c\n",*source);
- printf("%c\n",*copyObj);
- tempSource = source;
- tempObj = copyObj;
- //根据两个字符串的长度,分别采取不同的拷贝策略
- //两个字符串长度的不同,有必要考虑吗?
- //当source长度低于copyObj时,一直往source里面拷贝,会出现越界的问题吗?
- if(sourceLen >= copyObjLen){
- while(*tempObj){
- *tempSource = *tempObj;
- tempSource++;
- tempObj++;
- }
- *tempSource = *tempObj;
- }
- else{
- while(*tempSource){
- *tempSource = *tempObj;
- tempSource++;
- tempObj++;
- }
- }
- }
复制代码
|
|