"Summarize" the common string-handling functions in C language

Source: Internet
Author: User

-------------------------------------------------------------------------------------------

There are no string types in the C language, but there are many string-handling functions available for the convenience of manipulating strings. such as:

input and output string, copy of string, connection, comparison and other operation functions. It is important to note that in the use of input and output, i.e. gets,

The puts function should introduce the header file <stdio.h>. When using other operation functions, the header file <string.h> is introduced. In addition, the use of connected

When you copy a string function, you must ensure that the remaining space of the target character array is sufficient to drop the new string and string Beam sign ' + '.

-------------------------------------------------------------------------------------------


The C language code is as follows:

/*** function: Copy the string from src to DST. The end of the string is also copied together. **SRC, or it can be a string constant. */char* my_strcpy (CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) {     assert ( DST);      assert (SRC);          char  *ret = dst;         while  (*dst++ =  *src++)      {          ;      }     return  (ret);} /*** function: Connect the SRC string to the back of the string in DST, and delete the string flag "\" after DST. * * The return value of this function is the first address of DST. */char* my_strcat (CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) {     assert ( DST);      assert (SRC);     char *ret = dst;           /*     ** looking for ' e ', the last of the DST      */     while  (*DST)      {           dst++;     }     /* Performing copy */      while  (*dst++ = *src++)      {           ;     }     return  (ret);} /* ** function: Compares the strings in two arrays in ASCII order and returns the comparison result by the return value of the function. * * String 1 = string 2, return value =0;** string 2〉 string 2, return value 〉0;** string 1〈 string 2, return value 〈0. */INT&NBSP;MY_STRCMP (CONST&NBSP;CHAR&NBSP;*DST,&NBSP;CONST&NBSP;CHAR&NBSP;*SRC) {      ASSERT (DST);      assert (SRC);          while  (*DST&NBSP;==&NBSP;*SRC)      {           if  (*dst ==  ')            {&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;          return  (0);           }          dst++;           src++;     }      if  ((*DST&NBSP;-&NBSP;*SRC)  > 0)            return  (1);     else           return  (-1);} /*** function: The actual length of the string is measured (without the end of the string "\") and as a function return value. */int my_strlen (CONST&NBSP;CHAR&NBSP;*DST) {     assert (DST);          int ret = 0;          while  (*dst++)      {           ret++;     }     return  (ret);} /*** function: Determine if SRC is a sub-string of DST, and if so, returns the address of the first equal character, otherwise returns NULL. */       char* my_strstr (CHAR&NBSP;*DST,&NBSP;CHAR&NBSP;*SRC) {      assert (DST);      assert (SRC);           char *pdst = dst;     char *psrc  = src;     char *tmp = NULL;          if  (*src ==  ')            return  (DST);       /*     ** if the characters after the first are not equal, &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;**DST jumps to the position of the next character equal to the first time.      */     while  (*PDST)  &&  (*PSRC) )      {          tmp = pdst;                     while (*pdst == *psrc  &&  (*PSRC))            {                pdst++;                psrc++;          }           if  (*psrc ==  ')            {               return  ( TMP);          }           else          {                pdst = tmp;                psrc = src;                pdst++;           }     }     return  (NULL);}

--------------------------------------------------------------------------------------------

Dry tip: (void *) You can receive pointers of any type, but you must force type conversions if you want to use them at a time.

--------------------------------------------------------------------------------------------

This article is from the "Nobody" blog, please be sure to keep this source http://814193594.blog.51cto.com/10729329/1714371

"Summarize" the common string-handling functions in C language

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.