字符串

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// CppReference.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
using namespace std;

/*
 * 說明:字符串拷貝版本1
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯或者有重疊,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。
 */
char *strcpy_v1(char *dest , const char *src)
{
    //調試時,使用斷言,入口檢測
    assert( (dest!=NULL) && (src!=NULL) );
   
    //注意這裏的內存指向參數dest所在的內存,不是棧內存,因而可以在函數中返回
    char *to = dest;
   
    //主要操作在while條件中完成
    while( (*dest++ = *src++)!='\0')
    {
        NULL;   
    }
   
    //返回拷貝字符串首地址,方便連綴,比如strlen(strcpy(dest,"hello"))
    return to;
}

/*
 * 說明:字符串拷貝版本2
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。
 */
char *strcpy_v2(char *dest , const char *src)
{
    char *d = dest;
    char c;
   
    while((c=*src++) != '\0')
    {
        *(dest++)=c;
    }
   
    *dest='\0';
   
    return d;
}

/*
 * 說明:字符串拷貝版本2(你能找出錯誤的原因嗎)
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。
 */
char *strcpy_v2_error(char *dest , const char *src)
{
    char *d = dest;
    char c;
   
    while((c=*src++) != '\0')
    {
        *(d++)=c;
    }
   
    *d='\0';
   
    return d;
}


/*
 * 說明:字符串拷貝版本3
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。
 */
char *strcpy_v3(char *dest , const char *src)
{
    char *d = dest;
    char c;
   
    while(*src)
        *dest++ = *src++;
       
    *dest='\0';
   
    return d;
}

/*
 * 說明:字符串拷貝版本4
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。
 */
char *strcpy_v4(char *dest , const char *src)
{
    char *d = dest;
    char c;
   
    while( (*dest = *src)!='\0')
    {
        src++;
        dest++;
    }
       
    *dest='\0';
   
    return d;
}

/*
 * 說明:字符串拷貝版本5
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。restrict關鍵字限定字符串不能重疊。
 */
char *strcpy_v5(char* _restrict dest , const char* _restrict src)
{
    char *d = dest;
    char c;
   
    while( (*dest = *src)!='\0')
    {
        src++;
        dest++;
    }
       
    *dest='\0';
   
    return d;
}

/*
 * 說明:字符串拷貝版本6
 * 參數:dest目標地址,src源地址
 * 返回:返回拷貝好的地址;如果出錯,無定義
 * 異常:可能出現字符串溢出,及dest所佔空間不如src所佔空間大。restrict關鍵字限定字符串不能重疊。
 */
char *strcpy_v6(char* _restrict dest , const char* _restrict src)
{
    char *d = dest;
    char c;
   
    while(*dest++=*src++);
    return d;
}

 

int _tmain(int argc, _TCHAR* argv[])
{
    char buf[512];
   
    char *buf2 = (char *)calloc(50,sizeof(char));
   
    char *buf3 = (char *)malloc(50*sizeof(char));
   
    char *buf5 = (char *)malloc(50*sizeof(char));
   
    char *buf6 = (char *)malloc(50*sizeof(char));
   
    printf("using strcpy_v1,the string 'Hello,World'\'s length is : %d\n",strlen(strcpy_v1(buf,"Hello,World")));
   
    printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2(buf2,"This is the best age")));
   
    printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2_error(buf2,"This is the best age")));
   
    printf("using strcpy_v3,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v3(buf3,"This is the best age")));
   
    printf("using strcpy_v5,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v5(buf5,"This is the best age")));
   
    printf("using strcpy_v6,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v6(buf6,"This is the best age")));
 
    system("pause");
   
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章