Simulating the implementation of commonly used string functions

Source: Internet
Author: User

1. Find the implementation char *  my_strstr of substring function strstr (const char * dest, const char  * SRC)//const protection string is not changed {assert (dest); assert (SRC);      //assert char *  ptr1 = null;char * ptr2 = src;while  (*dest) {ptr1 = dest;// The same pointer while  (*DEST == *SRC) {dest++; src++;if) of the string is retained after the match succeeds after the position pointer src = ptr2;//retention match fails.   (*src ==  ') return ptr1;//match successfully, return to the first address}dest++;} return null;//matching Failure} Advantages: The program is simple and easy to understand disadvantages: the search efficiency is lower than 2, the simulation string copy function strcpy implementation char * my_strcpy (char *  DEST, CONST CHAR * SRC) {assert (dest); assert (SRC);//Assert const char *ret =  dest;//keep the target string's head pointer while  (*SRC) {*dest++ = *src++;//copy}*dest =  '} One by one;//Will ' \ 0 ' Copy return  ret;//returns the first address of the target string} disadvantage: the copy encountered ' \ ' at the end, and cannot copy some of the string containing '. For example: char arr[10] = {0,0,0,0,0};3, string length calculation function Strlen 3 implementations of function 1) Use counter Int my_strlen_count (const  CHAR * STR)//counter implements the string length calculation function {assert (str);//Assert int count = 0;while  (*STR) {count++; str++;} return count;//return length}2) use pointer subtraction int my_strlen_ptr (CONST CHAR * STR)//Use pointer subtraction to achieve string length calculation { ASSERT (str);//Assert const char * ret = str;while  (*str++) {;} return  (str - ret - 1);//return length}3) use function recursion to extract INT MY_STRLEN_RECU (const char  * STR)//Use recursion to calculate string length {assert (str);//Assert if  (*str !=  ') {return  (MY_STRLEN_RECU (str  + 1)  + 1);} elsereturn 0;} 4, the string connection function Strcat implementation Char * my_strcat (CHAR * DEST, CONST CHAR * SRC)// String Join function {assert (dest); assert (SRC);//Assert char *ret = dest;//retain the target string's head pointer while  (*dest) {dest++;} Find ';while  ' (*dest++ = *src++)//connect src to dest{;} return ret;//returns the first address of the target string}5, the implementation of the string comparison function strcmp int my_strcmp (const char *str1, const  CHAR *STR2)//string comparison function {assert (sTR1); assert (str2);//Assert while  (*STR1 == *STR2) {str1++;str2++;if  ('  == *str1 ') { return 0;//equal}}return *str1 - *str2;//, returns the difference of ASCII codes of unequal characters}


Simulating the implementation of commonly used string functions

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.