C-Language string manipulation function collation

Source: Internet
Author: User

#include <stdio.h> #include <string.h> #include <stdlib.h>int main () {     char *str1= "hello world!";     char *str2= "hello world!";     int len=strlen (STR1);//The string length to be asked, excluding '     printf ' ("len=%d\n" , Len);     int cmp=strcmp (STR1,STR2);     printf ("cmp=%d\n", CMP); &NBSP;&NBSP;&NBSP;&NBSP;//STR1&GT;STR2, returns a value greater than 0, STR1&LT;STR2 returns a value less than 0, which is equal to return 0. The comparison is by character comparison     //int strcmp (Char *str1,char * str2,int n), Just can control the number, other and strcmp () like     char str3[100]= "hello world!";     char str4[100]= "hello world!";     strcat (STR3,STR4);     printf ("str3=%s\n", STR3);    The  //function connects the string str4  to the end of the STR3, which erases the str3 end of ' STR4 ', but it retains and returns the pointer str3. The &NBSP;&NBSP;&NBSP;&NBSP;//STR4 should be const. STR3 space is enough to accommodate STR3 and STR4, or it will sendOverflow error, this function is unsafe     char str5[100]= "hello world!";     char str6[100]= "hello world!";     strncat (str5,str6,5);     printf ("str3=%s\n", STR5);     //This function, like strcat (), is only relatively safe and can explicitly control the number of link characters.     //will erase the STR5 ' ", and will automatically add '     char str7[100]= ' hello  World! ";     char str8[100]= "hello world!";     strcpy (STR7,STR8);     printf ("str7=%s\n", STR7);     //Copy the contents of the Str8 to STR7, including ' \ ', return the STR7 pointer, that is, all erase STR7 content, change to str     // The premise is that the STR7 space should be large enough to accommodate the contents of str8 and ' s ', so this function is relatively unsafe     char str9[100]= "hello world!";     char str10[100]= "hello worldddd!";     strncpy (str9,str10,14);     len=strlen (STR9);     printf ("str9=%s\n", STR9);     printf ("str9 len=%d\n", Len);    /*         : Copy  source  first  num  characters to  destination . If you encounter          to ';  ' in a copy  num  character, then fill ' + ' if  source The length of   is greater than  num, and after the  num  characters are copied, it is not          will be in   Add ' destination  ' to the back. In this case, the,destination  does not end with '/', read it may          be out of bounds.          the usual practice is to set the  n according to the size of  dest  and then read  N-1  characters. Then will  dest[N-1]=         ';    */   '   //Teacher's document as described above, but his own test several times without error, and with strlen () statistics is also correct     //use this function premise is also the target string space to be enough, It's just relative safety     int a=192,b=168,c=100,d=111;    char str11[18];     sprintf (Str11, "%d.%d.%d.%d", A,B,C,D);     len=strlen (STR11);     printf ("str11=%s\n", Str11);     printf ("str11 len=%d\n", len);     //function function: Writes formatted data to a string buffer. The premise is the buffer to be stored under the     char str12[100]= "&NBSP;&NBSP;&NBSP;123ABC";     int  data=atoi (Str12);     printf ("str12 to data=%d\n", data);     /*     *  function: Converts a string to an integer number; Atoi () scans the parameters  nptr  string, skipping the preceding space-word       *  until a number or sign is encountered to start the conversion, and then the non-numeric or string ('/') ends      *  conversions and returns the result (returns the converted integer number), and if the first is a character, it returns 0     *  in Stdlib.h     */     data=123;    char str13[100];    itoa ( data,str13,10)     printf ("base=10,str13=%s\n", Str13);     itoa (data, str13,2);     printF ("base=2,str13=%s\n", Str13),     /*     * converts the integer data to a ' A string that ends in a  str  to the character array that is pointed to.     */    char  in      * stdlib.h Str15[100]= "hello world!"; &NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;*P=STRCHR (Str15, ' l ');     printf ("L in str15  first=%s\n ", p);     //function: Returns the position pointer for the first occurrence of the character  c  in the string  str , not returning to the return   Null.     char str16[100]= "LL";     p=strstr (str15,str16);     printf ("str16 in str15 first=%s\n", p);     //function function: strstr ()   Function search string  str2  Whether it appears in string  str1 . When the searched character     //string is found, the function returns the address of the first matching string, or if the searched string is not found, returns null    char  Str17[100]= "[email protected]@[email protected]";     char *str18=strtok ( Str17, "@");nbsp;   printf ("str18=%s\n", Str18);    int i=0;     for (i=0;i<3;i++) {        str18=strtok (NULL, "@");         printf ("str18=%s\n", Str18);    }     /*     *  function: decomposes a string into a set of strings. s  the string to be decomposed as a delimiter string,delim .          Description:,s  points to the string to decompose when first called, and then calls to set  s  to  null.         strtok  find the characters contained in  delim  in  s  and use   NULL (',  ') to replace the entire string until it is searched.          return value: A split string from the beginning of the  s . Returns  null when there is no split string.          all the characters contained in  delim  are filtered out and set to a segmented node in the filter area.     */    char str19[20]={0};    //memset ( Str19, ' y ', sizeof (STR19));    memset (str19, ' Y ', 5);     printf ("memset str19=%s\n", Str19);     //function:  function copy ch  to buffer  count  Word character,  and returns the buffer pointer from the beginning.     //memset ()   can be applied to initialize a section of memory to a value.     //For example: memset ( the_array,  ',  sizeof (the_array)  );     //This is a convenient way to set the component of an array to 0.     char str20[100]= "hello world!";     char str21[100]= "hello world!";     memcpy (str20,str21,5);     printf ("memcpy str21 to  Str20=%s\n ", str20);     //definition: void *memcpy ( void *to, const void  *from, size_t count )     //function: The function copies the count  characters from the from into to and returns the to pointer. If to  and  from  overlap, the function behavior is indeterminate.     //applied to strings acts like strncpy ()     char str22[100]= "Hello woRld! ";     char str23[100]= "hello world!";     memmove (str22,str23,5);     printf ("Memmove str23 to  str22=%s\n ", str22),     //function:  and mencpy, different when to  and  from  overlap, The function will still work properly.     return 0;}


C-Language string manipulation function collation

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.