首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

什么样的strcopy才能得满分?解决思路

2012-05-07 
什么样的strcopy才能得满分?char* strcopy(char* ch1,char* ch2){assert(ch1 && ch2)if(!ch1 || !ch2)ret

什么样的strcopy才能得满分?
char* strcopy(char* ch1,char* ch2)
{
  assert(ch1 && ch2);
  if(!ch1 || !ch2)return ch1;
  int size1=strlen(ch1);
  int size2=strlen(ch2);
  if(size1<size2)return ch1;
  char* q=ch1,*p=ch2;
  while(*(q++)=*(p++));
  *q='\0';
  return ch1;
}
这个可以吗?
如果不能大家可以发发自己的版本

[解决办法]
差远了···
1.变量的命名方式
2.第二个参数用const
3.if(size1<size2)return ch1;//返回ch1???
4.*q='\0';//不需要再补一个\0了
5.楼下大牛补
[解决办法]

探讨

标准的是这样的吧
char* strcopy(char* ch1,const char* ch2)
{
assert(ch1&amp;&amp; ch2);
char* q=ch1;
while(*ch1++=*ch2++);
return q;
}
这里没有判断ch1长度是否比ch2短 ,会发生非法访问

[解决办法]
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\strcat.asm
Assembly code
        page    ,132        title   strcat - concatenate (append) one string to another;***;strcat.asm - contains strcat() and strcpy() routines;;       Copyright (c) Microsoft Corporation. All rights reserved.;;Purpose:;       STRCAT concatenates (appends) a copy of the source string to the;       end of the destination string, returning the destination string.;;*******************************************************************************        .xlist        include cruntime.inc        .listpage;***;char *strcat(dst, src) - concatenate (append) one string to another;;Purpose:;       Concatenates src onto the end of dest.  Assumes enough;       space in dest.;;       Algorithm:;       char * strcat (char * dst, char * src);       {;           char * cp = dst;;;           while( *cp );                   ++cp;           /* Find end of dst */;           while( *cp++ = *src++ );                   ;               /* Copy src to end of dst */;           return( dst );;       };;Entry:;       char *dst - string to which "src" is to be appended;       const char *src - string to be appended to the end of "dst";;Exit:;       The address of "dst" in EAX;;Uses:;       EAX, ECX;;Exceptions:;;*******************************************************************************page;***;char *strcpy(dst, src) - copy one string over another;;Purpose:;       Copies the string src into the spot specified by;       dest; assumes enough room.;;       Algorithm:;       char * strcpy (char * dst, char * src);       {;           char * cp = dst;;;           while( *cp++ = *src++ );                   ;               /* Copy src over dst */;           return( dst );;       };;Entry:;       char * dst - string over which "src" is to be copied;       const char * src - string to be copied over "dst";;Exit:;       The address of "dst" in EAX;;Uses:;       EAX, ECX;;Exceptions:;*******************************************************************************        CODESEG%       public  strcat, strcpy      ; make both functions availablestrcpy  proc \        dst:ptr byte, \        src:ptr byte        OPTION PROLOGUE:NONE, EPILOGUE:NONE        push    edi                 ; preserve edi        mov     edi,[esp+8]         ; edi points to dest string        jmp     short copy_startstrcpy  endp        align   16strcat  proc \        dst:ptr byte, \        src:ptr byte        OPTION PROLOGUE:NONE, EPILOGUE:NONE        .FPO    ( 0, 2, 0, 0, 0, 0 )        mov     ecx,[esp+4]         ; ecx -> dest string        push    edi                 ; preserve edi        test    ecx,3               ; test if string is aligned on 32 bits        je      short find_end_of_dest_string_loopdest_misaligned:                    ; simple byte loop until string is aligned        mov     al,byte ptr [ecx]        add     ecx,1        test    al,al        je      short start_byte_3        test    ecx,3        jne     short dest_misaligned        align   4find_end_of_dest_string_loop:        mov     eax,dword ptr [ecx] ; read 4 bytes        mov     edx,7efefeffh        add     edx,eax        xor     eax,-1        xor     eax,edx        add     ecx,4        test    eax,81010100h        je      short find_end_of_dest_string_loop        ; found zero byte in the loop        mov     eax,[ecx - 4]        test    al,al               ; is it byte 0        je      short start_byte_0        test    ah,ah               ; is it byte 1        je      short start_byte_1        test    eax,00ff0000h       ; is it byte 2        je      short start_byte_2        test    eax,0ff000000h      ; is it byte 3        je      short start_byte_3        jmp     short find_end_of_dest_string_loop                                    ; taken if bits 24-30 are clear and bit                                    ; 31 is setstart_byte_3:        lea     edi,[ecx - 1]        jmp     short copy_startstart_byte_2:        lea     edi,[ecx - 2]        jmp     short copy_startstart_byte_1:        lea     edi,[ecx - 3]        jmp     short copy_startstart_byte_0:        lea     edi,[ecx - 4];       jmp     short copy_start;       edi points to the end of dest string.copy_start::        mov     ecx,[esp+0ch]       ; ecx -> sorc string        test    ecx,3               ; test if string is aligned on 32 bits        je      short main_loop_entrancesrc_misaligned:                     ; simple byte loop until string is aligned        mov     dl,byte ptr [ecx]        add     ecx,1        test    dl,dl        je      short byte_0        mov     [edi],dl        add     edi,1        test    ecx,3        jne     short src_misaligned        jmp     short main_loop_entrancemain_loop:                          ; edx contains first dword of sorc string        mov     [edi],edx           ; store one more dword        add     edi,4               ; kick dest pointermain_loop_entrance:        mov     edx,7efefeffh        mov     eax,dword ptr [ecx] ; read 4 bytes        add     edx,eax        xor     eax,-1        xor     eax,edx        mov     edx,[ecx]           ; it's in cache now        add     ecx,4               ; kick dest pointer        test    eax,81010100h        je      short main_loop        ; found zero byte in the loop; main_loop_end:        test    dl,dl               ; is it byte 0        je      short byte_0        test    dh,dh               ; is it byte 1        je      short byte_1        test    edx,00ff0000h       ; is it byte 2        je      short byte_2        test    edx,0ff000000h      ; is it byte 3        je      short byte_3        jmp     short main_loop     ; taken if bits 24-30 are clear and bit                                    ; 31 is setbyte_3:        mov     [edi],edx        mov     eax,[esp+8]         ; return in eax pointer to dest string        pop     edi        retbyte_2:        mov     [edi],dx        mov     eax,[esp+8]         ; return in eax pointer to dest string        mov     byte ptr [edi+2],0        pop     edi        retbyte_1:        mov     [edi],dx        mov     eax,[esp+8]         ; return in eax pointer to dest string        pop     edi        retbyte_0:        mov     [edi],dl        mov     eax,[esp+8]         ; return in eax pointer to dest string        pop     edi        retstrcat  endp        end 


[解决办法]

探讨

C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\strcat.asm
Assembly code
page ,132
title strcat - concatenate (append) one string to another
;***
;strcat.asm - c……

[解决办法]
老版的strcpy函数没有字符串长度的安全检查,新版的strcpy_s函数才有字符串长度的类型安全检查,况且老版的strcpy函数有2个参数,而新版的strcpy_s函数有3个参数。

下面是满分的strcpy()函数:

C/C++ code
#include <assert.h>char *strcpy(char *strDest,const char * strSrc){ assert(strDest && strSrc); char *dest = strDest; while((*strDest++ = *strSrc++) != '\0'); return dest; }
[解决办法]
按楼主的做法 如果我定义了一个char c[1000];
然后c[0]=0;这样楼主的函数不就费了么
[解决办法]
大家认为有没有必要做自复制的判断?
C/C++ code
if (strDest == strSrc)    return strDest;
[解决办法]
我也来一遍吧strcpy
char* strcpy(char* strDest,char* strSrc)
{
assert((strDest != NULL) && (strSrc != NULL));
char *address=strDest;
while((*strDest++=*strSrc++) != '\0 ')
{

}

return address;
}
[解决办法]
char* strcpy(char* strdest,const char* strsrc)
{
assert(strdest&&strsrc);
if(strdest==strsrc)
return strdest;
char* pstr=strdest;
while((*pstr++=*strsrc++)!='\0')
{
NULL;
}
return strdest;
}

热点排行