String Common functions

Source: Internet
Author: User
Tags sprintf strcmp strtok

Just pass the code ...

#include <iostream>#include<string.h>#include<stdlib.h>voidDifferfromstrlenandsizeof (void);voidStringcontact (void);voidStringcompare (void);voidStringcopy (void);voidFormatString (void);voidFindspicified (void);voidSplitstring (void);voidTurninto (void);intMainintargcChar**argv) {        //differfromstrlenandsizeof (); //stringcontact (); //Stringcompare (); //stringcopy (); //formatString (); //splitstring ();Turninto (); return 0;}voidTurninto (void){    //contained within the Stdlib .    intA = Atoi (" at"); printf ("%d\n", a); floatb = Atof ("23.4"); printf ("%f\n", B); LongC =atol ("123456789"); printf ("%d\n", c); }//String SegmentationvoidSplitstring (void){    CharBuf[] ="[email protected] @ghi"; Char*p = Strtok (buf,"@");  while(p) {printf ("%s\n", p); P=strtok (NULL,"@"); }} /** Find the specified element*/voidFindspicified (void){        Chars[Ten]="ABCDEFG"; printf ("%d\n", STRCHR (S,'D'));//the value of the underlying returns 3printf"%d\n", *STRCHR (S,'D')); printf ("%c\n", *STRCHR (S,'D')); printf ("%d\n", STRCHR (S,'P')); printf ("%d\n", Strstr (S,"de")); printf ("%d\n", Strstr (S,"DF")); //not very good (Hui) with             }/** Formatted string*/voidFormatString (void){    //as with printf () basically, just sprintf () This function simply resembles a char[] array to output a string        Charbuf[Ten]; sprintf (BUF,"asdf");//There is an overflow problemsprintf (BUF,"%d",3);//and will also coverprintf (BUF); }/** String Copy*/ voidStringcopy (void){        CharS1[] ="123"; Chars2[Ten];    strcpy (S2,S1);    printf (S2); //try the extra long copy .    Charslong[ -] ="ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"; //if not wrong, it is because it is not long enough, but as long as this is the hidden danger, hidden danger if not now, sooner or later will be out//so generally look at C write how, put this program continuously open a week, how many times per day how many times of execution, see will crash. //if it's all right, it's basically fine.    If anything, we'll have to look at these places. //for the industry, some programs have been running for more than a year, but the time is a bit longer and may not be possible.         There's going to be a situation like this. //strcpy (S2,slong); //printf (S2); //so in order to avoid this, as prepared, strncpy () string Copy command, need to pass three parameters, copy length. //This will not be tried, but the same requirements. is sizeof (S2)-strlen (S2)-1; "But I always thought it would be better to copy one at the end."strncpy (S2,slong,sizeof(S2)-strlen (S2)-1);    printf (S2); } /** Full comparison of strings strcmp the finite bit comparison of strings strncmp more than one parameter returns the result of the word typeface minus, front-back a-b<0 so return-1, with the corresponding ASCII code. */voidStringcompare (void){    Chars1[]="a"; Chars2[]="b"; printf ("%d\n", strcmp (S1,S2));//-1    CharS3[] ="C"; printf ("%d\n", strcmp (S3,S2));//1printf"%d\n", strcmp (S1,S1));//0printf"%d\n",sizeof(S3));//2        Charss1[]="Lifei1"; Charss2[]="LIFEAA"; printf ("%d\n", strncmp (Ss1,ss2,4));//0printf"%d\n", strncmp (Ss1,ss2,5));//1}/** The value returned by sizeof is found to be 6 digits.    And strlen is 5 numbers. and sizeof feeds back the size of the memory space that this thing opens up, and strlen only returns the part of the pure character. */voidDifferfromstrlenandsizeof (void){    Chars[]="Lifei"; printf ("sizeof (s) is:%d\nstrlen (s) is:%d\n",sizeof(s), strlen (s)); Chars10[Ten]="Comeon"; printf ("sizeof (S10) is:%d\nstrlen (S10) is:%d",sizeof(S10), strlen (S10));}/** Character Append operation*/voidStringcontact (void){    Charsa[Ten] ="123"; CharRb: -] ="ABC";    strcat (SA,SB);    printf (SA); printf ("\ n"); Charsc[]="123456789ddddddddddddddddddddddddddddddddddddddddddddddddd"; //strcat (SA,SC); This is going to be an anomaly .//printf (SA); //strncat (sa,sc,sizeof (SA)-strlen (SA)); The space allocated to the SA for the contents of the space-sa. //There seems to be no error here, but in fact, there is no end to the string because it is not, so it is inherently a false statement but does not cause errors. //strncat (sa,sc,11); Direct error, the reason is: when the stitching is complete, the length of the SA is 17 my operating system is 8-bit. //so at the time of the output address is always 16 0, each one is 16 binary. So I want to add this number first, and then instantly report a forced stop.//strncat (sa,sc,10); it's been in the output, and it scares me. Ctrl + C shut down the program.//strncat (sa,sc,9); and it looks like it's working.//and this result is extremely unstable. There is a certain chance of a mistake. After I have written some notes, the result will not be tried out, but you can then add this number to the top,//The exception will reappear when he is crowding out a memory address that someone else will use, such as adding a 16-bit//Well, I was actually going to say that you should leave it at the end of a character. //sizeof (SA)-strlen (SA)-1, that one 1 is reserved for. Strncat (SA,SC,sizeof(SA)-strlen (SA));    printf (SA); sizeof(SA); printf ("\n%d",sizeof(SA)); //It always feels like there's a problem here because sizeof (SA)-strlen (sa) =4. Actually, we just put 123456789 in SA and there's no problem. //so you shouldn't have put so much data, if you add a limit. I want to know where it's not going to go wrong//not figured out, this number is tried out.          }

String Common 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.