Self-write string manipulation functions

Source: Internet
Author: User
Tags strcmp

"Character manipulation functions"


The 1.strstr () function is used to retrieve the first occurrence of a substring in a string, and its prototype is:
Char *strstr (const char *dest,const char * src);

The parameter description dest is the string to retrieve, and SRC is the substring to retrieve.

"Return value" returns the address of the first occurrence of the substring src in str , or null if no substring is retrieved.


"Function Implementation"

#include  <stdio.h>char* my_strstr (CONST&NBSP;CHAR&NBSP;*DEST,CONST&NBSP;CHAR&NBSP;*STR) {      char *ptr= (char *) dest;     char *p1=null;      char *p2=NULL;     while  (*ptr)       {         p1=ptr;          p2= (char *) str;         while  ( *P1==*P2)         {              p1++;              p2++;             if  (*p2== ')             {                  return ptr;             }        }         ptr++;     }     return null;}

Results:

650) this.width=650; "title=" Wh8qz%0ua%d5%d[ob%22kq2.png "alt=" wkiom1zeel-yykayaaaooufndeo897.png "src="/HTTP/ S4.51cto.com/wyfs02/m00/76/e3/wkiom1zeel-yykayaaaooufndeo897.png "/>

The 2.strcat () function is used to concatenate strings with the following prototypes:

Char *strcat (char *dest, const char *SRC)


The "parameter" Dest is the destination string pointer, and SRC is the source string pointer.


strcat () copies the parameter src string to the end of the string referred to by the parameter dest, and the final end character of Dest is overwritten, and a null is added to the tail of the concatenated string.

Note: The memory space referred to by dest and SRC cannot overlap, and dest should have enough space to accommodate the string to be copied.

Return value returns the start address of the dest string.

"Function Implementation"

#include <stdio.h> #include <assert.h> #define MAXLINE 100char* my_strcat (char *dest,const char *src) {Char     *pstr=dest;     ASSERT (dest);     ASSERT (SRC);     while (*dest) {dest++;     } while (*dest++=*src++) {; } return pstr;}     int main () {char arr[maxline]= "ABCD";     Char *p= "EFGH";     Char *ret=my_strcat (ARR,P);     printf ("%s\n", ret); return 0;}


Results:

650) this.width=650; "title=" G@i ' 1bv@ $CC 8]lt9yz ' 1b78.png "alt=" wkiol1zef9vjvib6aaapbgpw1am860.png "src="/HTTP/ S1.51cto.com/wyfs02/m02/76/e2/wkiol1zef9vjvib6aaapbgpw1am860.png "/>


The 3.strcmp () function is used to compare the string size, and its prototype is:

int strcmp (const char *dest, const char *src)

The "parameter" DEST,SRC is the two string that needs to be compared.

The comparison of the string size is determined in the order of the ASCII code table, which is also the value of the character. strcmp () First subtracts the first character value of Dest from the first character value of SRC, and if the difference is 0 then continues to compare the next character, and if the difference is not 0, the difference is returned. For example, the string "Ac" and "ba" comparisons return the difference between the characters "A" (65) and ' B ' (98) (-33).

"Return value" returns 0 if the parameter dest and the SRC string are the same. Dest values greater than 0 are returned if they are greater than SRC. A value less than 0 is returned if the dest is less than SRC.

Note: strcmp () is compared in binary mode

"Function Implementation"

#include  <stdio.h> #include  <assert.h>int my_strcmp (const char *str1,const &NBSP;CHAR&NBSP;*STR2) {     assert (str1);      assert (STR2);      while  (*STR1&NBSP;==&NBSP;*STR2)      {           if  (*str1 ==  ')           {                return 0;         }          str1++;         str2++;      }     return  (*STR1&NBSP;-&NBSP;*STR2);} Int main () {     char *str1= "ABCDE";     char  *str2= "Abcfe";     INT&NBSP;RET&NBSP;=&NBSP;MY_STRCMP (STR1,STR2);     if  (ret > 0)      {          printf ("str1  > str2\n ");      }     else if (ret <  0)      {          printf (" str1 < str2\n ");     }     else           printf ("str1 =str2\n");      return 0;}

Results:

650) this.width=650; "title=" (CZNWH4ZH1ET0M$O2DNL ' 35.png "alt=" wkiom1zeor2ifbupaaaqerzos7e669.png "src=" http:// S3.51cto.com/wyfs02/m01/76/e7/wkiom1zeor2ifbupaaaqerzos7e669.png "/>

The 4.strlen () function is used to calculate the length of a string, whose prototype is:

size_t strlen ( const char *string );

The parameter description string is the specified string.

Strlen () is used to calculate the length of the specified string, not including the ending character "\".

"Return value" returns the number of characters in a string.


The "note" strlen () function calculates the actual length of the string and encounters the end of the first ' s '. If you only define that you do not assign an initial value to it, the result is uncertain, and it will continue to be searched from the first address until it encounters a stop. sizeof returns the amount of memory the variable declaration takes, not the actual length, and sizeof is not a function, just an operator, strlen () is a function.

"Function Implementation"

/** Counter Method Implementation */#include  <stdio.h> #include  <assert.h>int my_strlen (Const char &NBSP;*STR) {     int count=0;     assert (str);//Assertion      while  (*STR)      {             count++;             str++;     }     return count;} Int main () {      char arr[]= "ABCDE";       int ret=my_strlen (arr);       printf ("%d\n", ret);       return 0;} /*** Recursive method Implementation */#include  <stdio.h> #include  <assert.h>int my_strlen (Const char &NBSP;*STR) {      assert (str);      if  (*str= = '  & ')Nbsp;    {            return  0;      }      else       {            return  (1+my_strlen ( str+1));       }}int main () {      char  arr[] =  "ABCDE";       int ret = my_strlen (arr);       printf ("%d\n", ret);       return 0;} /*** pointer mode implementation */#include  <stdio.h>int my_strlen (CONST&NBSP;CHAR&NBSP;*STR) {        const char *p=str;      while  (*STR)       {             Str++;     &nBSP;}       return str-p;} Int main () {      char arr[]= "ABCDE";       int ret=my_strlen (arr);       printf ("%d\n", ret);       return 0;}

Results:

650) this.width=650; "title=" s9{kc2c ' H_e_j~i[nns@4em.png "alt=" wkiol1zepdqduh_raaapd59g4jo548.png "src=" http:/ S1.51cto.com/wyfs02/m01/76/e6/wkiol1zepdqduh_raaapd59g4jo548.png "/>

5.strcpy () function, function prototype

Char *strcpy (char *dest, const char *SRC);

"Function description": strcpy () copies the parameter SRC string to the address referred to by the parameter dest.

Return value: Returns the string start address of the parameter dest.


"Function Implementation"

#include <stdio.h> #include <assert.h> #define MAX 20void my_strcpy (char *dest,char *str) {assert (dest);     ASSERT (str); while (*dest++=*str++);}     int main () {char arr[max]={0};     Char *ps= "ABCDE";     my_strcpy (ARR,PS);     printf ("%s\n", arr); return 0;}

Results:

650) this.width=650; "title=" tb][@F48Y5U8P%2ma1z$ix1.png "alt=" wkiom1zeprbg3elaaaaqbljlwb4924.png "src="/HTTP/ S2.51cto.com/wyfs02/m01/76/e7/wkiom1zeprbg3elaaaaqbljlwb4924.png "/>


Note: Why is the result abcde? The following 0 why did not print out, because the string is to ' "" as the end of the sign, and the corresponding ASCII code value is exactly 0, so the natural result is ABCDE!

This article is from the "Pzd Chuan Feng" blog, please make sure to keep this source http://xujiafan.blog.51cto.com/10778767/1718912

Self-write string manipulation 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.