Strlen three ways to implement the strcmp,strcat,strcpy

Source: Internet
Author: User

First, let's take a look at the strlen function that we are often familiar with in our study.

Strlen 1: Pointer difference value returned

int My_strlen (const char *str) {char *pst = (char *) str;             ASSERT (str);            while (*STR) {str++; } return str-pst;}

STRLEN2: Recursive implementation

int my_strlent (const char *str) {assert (str);             if (*STR) return 1+my_strlent (str+1); else return 0;}

Strlen3: Counter Control

int My_strleno (const char *str) {int count = 0;             ASSERT (str);            while (*str++) {count++; } return count;}

STRCMP's writing:

INT&NBSP;STRCMP (CONST&NBSP;CHAR&NBSP;*STR,&NBSP;CONST&NBSP;CHAR&NBSP;*DSTR)          //judgment function, does not allow changing parameters y{              int ret = 0;                      //definition return value              assert ((str != null) && (dstr != null));         //D Break               while (! ( ret =* (unsigned char*) str - * ( unsigned char *) Dstr)  &&  DSTR)                     //calculates the value of RET: o if?? The value of the string is &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;                                         //the same as A0, greater than, less than a-1;             {                         str++;                                  //when judging the end of a string;                         Dstr++;             }              if (ret < 0)                          ret = -1;              else if  (ret > 0)                           ret = 1;             return  ret;}

    strcpy:

#include  <stdio.h> #include  <assert.h>char *my_strcpy (char *dstr,char  CONST&NBSP;*SSTR) {                        char *Astr =  (char *) dstr;             assert (dstr != null)  &&   (sstr != null));              while ((*DSTR&NBSP;=&NBSP;*SSTR)  !=  ' + '  )              {                         Dstr++;                         Sstr++;             }              return astr;}

Strcat:

Char *my_strcat (CHAR&NBSP;*DSTR,CHAR&NBSP;CONST&NBSP;*SSTR) {              char *Astr =  (char *) dstr;             assert (dstr != null)  &&  (Sstr  != null));              while (' + ') &NBSP;!=&NBSP;*DSTR)                                       dstr++;            *dstr =   '   ';            dstr++;              while ('  != *sstr ')              {                                                  *dstr++  = *Sstr++;            }             *Dstr =  ';     '          return astr;}

Well, this blog post is like this, and it's all about the string manipulation functions that we use, and then we all pay attention to the concise mode of assert and writing. Yes, that's it.

This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1696331

Strlen three ways to implement the strcmp,strcat,strcpy

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.