Strlen (); strcpy (); Strcat (); STRCMP ()---Notes

Source: Internet
Author: User
Tags strcmp

Pointers Small Knowledge Points:

int a = 10;

int *p=&a;

int *q=p; The address of A is saved in P

int *q=p; Assigning the value of p to the Q function is to let Q also point to a

Strlen (); To find the length of a string

strcpy (); Copy string

Strcat (); Connection string

strcmp (); Comparison of string sizes

1typedef unsignedintsize_t2 3size_t My_strlen (Const Char*str)//strlen ()4 {5ASSERT (str!=NULL);6      intlen=0;7       while(*str! =' /')    8      {9len++;Tenstr++; One      } A        returnLen; - } -  the //recursion is not used with intermediate variables; -size_t My_strlen (Const Char*str) - { -ASSERT (str!=NULL); +       if(*str==' /') -              return 0; +       Else A           returnMy_strlen (++STR) +1;  at}

1 Char* MY_STRCPY (Char*dest,Const Char*src)//strcpy ()2 {3ASSERT (dest!=null&&src!=NULL);4      Char*dest_tmp=dest;//Protection Parameters5      Const Char*SRC_TMP=SRC;//type Match6       while(*src_tmp!=' /')7     {8*dest_tmp++=*src_tmp++;9      }Ten  One*dest_tmp=' /'; A            returndest; - } -  the voidMain () - { -       Chars1[ -]; -       Char*S2 ="ABCDE"; + my_strcpy (S1, S2); -cout<<"S1 ="<<s1<<endl;//ABCDE//c++ Output mode + } A //or a third party accepts at voidMain () - { -       Chars1[ -]; -       Char*S2 ="ABCDE"; -       Char*s3=my_strcpy (S1, S2); -cout<<"s3 ="<<s3<<endl;//ABCDE in}
1 Char* MY_STRCAT (Char*dest,Const Char*src)//strcat ()2 {3ASSERT (src!=null&&dest!=NULL)4   Char*dest_tmp=dest;//Protection Parameters5   Const Char*SRC_TMP=SRC;//type Match6   7     while(*dest_tmp!=' /') 8dest_tmp++;9     while(*src_tmp!=' /')Ten*dest_tmp++=*src_tmp++; One*dest_tmp=' /'; A returndest; - } -  the voidMain () - { -     Chars1[ -] ="abcdef"; -     Chars2[ -] ="Ghijk"; +     Char*STR1 =my_strcat (S1, S2); -cout<<"S1 ="<<s1<<Endl; +cout<<"str1 ="<<str1<<endl;//Abcdefghijk//c++ Output mode A  at}
1 /*strcmp (S1,S2); 2 s1>s2: Return 13 S1=s2: return 04 S1<S2: Return-15 */6 7 intMY_STRCMP (Const Char*STR1,Const Char*str2)//strcmp ()8 {9ASSERT (Str1!=null && str2!=NULL);Ten     if(*str1==' /'&& *str2==' /') One         return 0; A      while(*str1!=' /'&& *str2!=' /') -     { -         if(*str1 > *str2) the             return 1; -         Else if(*str1 < *str2) -             return-1; -str1++; +str2++; -     } +     if(*str1==' /'&& *str2!=' /') A         return-1; at     if(*str1!=' /'&& *str2==' /') -         return 1; -     return 0; - } -  - //or in intMY_STRCMP (Const Char*STR1,Const Char*str2) - { toASSERT (Str1!=null && str2!=NULL); +     intresult =0; -      while(*str1!=' /'|| *str2!=' /') the     { *         if(result = *STR1-*STR2)! =0 ) $              Break;Panax Notoginsengstr1++; -str2++; the     } +     if(Result >0 ) Aresult =1; the     Else if(Result <0 ) +result =-1; -     returnresult; $}

Attention:

1, the validity of judging parameters;

(assertion) The argument passed cannot be null.

2, if need to return the pointer, the use of intermediate variables;

my_strcpy (); My_strcat ();

3. Ensure sufficient space

my_strcpy (); My_strcat ();

4, parameter protection to ensure that the parameters are not modified inside the function

Strlen (); strcpy (); Strcat (); STRCMP ()---Notes

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.