Implementation of string library functions

Source: Internet
Author: User

// String-series library functions: string = character array + '\ 0 ';
1 char * strcpy1 (char * strDest, const char * strSrc );
2 char * strncpy1 (char * strdest, const char * strsrc, int n );
3 int strcmp1 (const char * str1, const char * str2 );
4 int strlen1 (const char * str); // you can also use recursive thinking.
5 char * strstr1 (const char * str1, const char * str2 );
6 char * strcat1 (char * strDest, const char * strSrc );
7 void * memcpy1 (void * pvTo, const void * pvFrom, size_t size); // copy the memory


========================================================== ========================================================== ==========

1 char * strcpy1 (char * strDest, const char * strSrc );

Char * strcpy1 (char * strDest, const char * strSrc) {assert (strSrc! = NULL & strDest! = NULL); char * str = strDest; // while (str! = '\ 0') while (str! = NULL) The same mistake !!! While (* strSrc! = '\ 0') {* str ++ = * strSrc ++;} * str =' \ 0 '; // end with the string ending mark cout <strDest <endl; return strDest ;}



2 char * strncpy1 (char * strdest, const char * strsrc, int n)

Char * strncpy1 (char * strdest, const char * strsrc, int n) {assert (strdest! = NULL & strsrc! = NULL); char * str = strdest; int num = 0; while (* strsrc! = '\ 0' & num <n) {* str ++ = * strsrc ++; num ++;} if (num = n) {* str = '\ 0'; cout <"Copied successfully:" <strdest <endl; return strdest ;}cout <"Copied failed !!! "<Endl; return NULL ;}




3 int strcmp1 (const char * str1, const char * str2)

// Negative: Compare the value of each character. Positive values return 1. equal values return 0. Negative values return-1. int strcmp1 (const char * str1, const char * str2) {assert (str1! = NULL & str2! = NULL); int res = 0; // while (* s1 = * s2) {} res = * s1-* s2; while (! (Res = (unsigned char *) * str1-(unsigned char *) * str2) & (* str2) {str1 ++; str2 ++ ;} if (res> 0) {cout <1 <endl; return 1;} else if (res <0) {cout <-1 <endl; return-1 ;}cout <0 <endl; return 0 ;}



4 int strlen1 (const char * str)


Int strlen1 (const char * str) {assert (str! = NULL); int num = 0; const char * strtmp = str; // char * strtmp = str; error while (* strtmp! = '\ 0') {num ++; strtmp ++;} cout <"The number is" <num <endl; return num ;}




5 char * strstr1 (const char * str1, const char * str2)


Char * strstr1 (const char * str1, const char * str2) {assert (str1! = NULL & str2! = NULL); // const char * p = str1, * p2 = str2; char * p = (char *) str1, * p2 = (char *) str2; char * tmp = NULL; // tmp starts from the first matched character. While (* p! = '\ 0') {if (* p = * p2) {tmp = p; while (* tmp = * p2 & * p2! = '\ 0') {tmp ++; p2 ++;} if (* p2 =' \ 0') {cout <"match: "<p <endl; return p ;}} p ++ ;}cout <" failed to match! "<Endl; return NULL ;}



6 char * strcat1 (char * strDest, const char * strSrc)

// 1. Find the last position of strDest. 2. Create a one-to-one copy of the connection. Char * strcat1 (char * strDest, const char * strSrc) {/* assert (strDest! = NULL & strSrc! = NULL); int lenDest, lenSrc; lenDest = strlen (strDest); lenstrlen (strSrc); for (int I = 0; I <lenSrc; I ++) {strDest [lenDest + I] = strSrc [I];} strDest [lenDest + lenSrc] = '\ 0'; cout <strDest <endl; return strDest; * // Method 2: assert (strDest! = NULL & strSrc! = NULL); char * strP = strDest; while (* strP! = '\ 0') {strP ++;} // copy from the last position. While (* strSrc! = '\ 0') {* strP ++ = * strSrc ++;} * strP =' \ 0'; cout <strDest <endl; return strDest ;}




7 void * memcpy1 (void * pvTo, const void * pvFrom, size_t size); // copy the memory

Note: An error occurred during compilation !!

/// Memory copy can copy any data type --- convert to (byte *) // void * memcpy1 (void * pvTo, const void * pvFrom, size_t size) // {// assert (pvTo! = NULL & pvFrom! = NULL); // byte * memTo = (byte *) pvTo; // byte * memFrom = (byte *) pvFrom; // while (size> 0) // {// * memTo ++ = * memFrom ++; // size --; //} // return pvTo ;//}



Test code:



Char str1 [100], str2 [] = "nihao haha"; // strcpy1 (str1, str2); strncpy1 (str1, str2, 8 ); /* char * s1 = "Hello, world! "; Char * s2 =" Hello, World! "; Strcmp1 (s1, s2); * // strlen1 (str2); // outside the function, it can be: len = sizeof (str)/sizeof (char ); /* char * s1 = "Hello, world! "; Char * s2 =" world "; str1 (s1, s2); */char string1 [50] =" Thanks, thank you. "; char * s =" you are welcome. "; strcat1 (string1, s );


Related Article

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.