Implementation of strcpy functions and string classes

Source: Internet
Author: User
1 strcpy function implementation
void* memcpy1(void *dst,const void *src,int count){assert(dst != NULL && src!= NULL && count>=0);void *temp = dst;char *pdst = (char*)dst;char *psrc = (char*)src;if(pdst>=psrc && pdst <= psrc+count-1){pdst = pdst + count -1;psrc = psrc + count -1;while (count--)*pdst-- = *psrc--;}else{while(count--)*pdst++ = *psrc++;}return temp;}char* strcpy1(char *dst,const char *src){assert(dst != NULL && src!= NULL);char *temp = dst;memcpy1(dst,src,strlen(src)+1);return temp;}

2 partial implementation of the string class
class MyString{public:friend ostream& operator<<(ostream&,const MyString&);MyString(const char *str = NULL){if(NULL == str)m_data = 0;else{m_data = new char[strlen(str)+1];strcpy(m_data,str);}}MyString(const MyString & string){if(string.m_data == 0 || string.m_data == NULL)m_data = 0;else{m_data = new char[strlen(string.m_data)+1];strcpy(m_data,string.m_data);}}MyString& operator=(const MyString &string){if(this != &string){char *temp = m_data;if(string.m_data==0 || string.m_data == NULL)m_data = 0;else{m_data = new char[strlen(string.m_data)+1];strcpy(m_data,string.m_data);}delete []temp;}return *this;}private:char *m_data;};


Implementation of strcpy functions and string classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.