C String manipulation functions

Source: Internet
Author: User
Tags strtok

String for C-style (char p[n]; ):

Length (strlen), append (strcat, strncat), compare (strcmp, strncmp), lookup (STRCHR , strstr) and so on.

-the version with N is a limited operation, not all. For example, strcmp compares two strings, while strncmp compares the first n characters of two strings.

--Append should use strncat, because strcat is in danger of overflow. (Oddly enough,strcat in my mingw32-gcc 4.9.3 here without prompting for errors.) )

Input (scanf,gets,fgets,sscanf), Output (printf,puts,fputs ,sprintf).

-scanf also has the possibility of overflow, and will be blank (space, carriage return, etc.) as input termination.

-gets will only terminate the carriage return as input. But it also exists overflow possible.

--Thefgets is safe.

--sscanf is to read the contents of the string into the specified variable! The opposite of sprintf .

--sprintf is the output of a string of content to the specified strings!!

There is also a atoi function that converts a string to int . (atof--turn into floating point, others slightly)

--a should be the meaning of the char array.

Note: The above-mentioned overflow is a result of the length of the target character array that cannot accommodate the final result.

The code is as follows:

#include <stdio.h>#include<stdlib.h>#include<string.h>intMainintargcChar Const*argv[]) {    Chars1[ the]="Hello World"; Chars2[ the]="ABC defxxx"; strcat (s1, S2); //Append//strncat (S1, S2, strlen (S2));printf"%s\n", S1); intv = strcmp (s1, S2);//Compareprintf"%d\n", V); Chars3[]="Hello"; Chars4[]="Hello"; if(S3! = S4) {//the address should be compared.printf"%s! =%s\n", S3, S4); }    Chars5[]="helloaaa"; Chars6[]="hellobbb"; V= strncmp (S5, S6,5);//Comparison of the first 5 charactersprintf"%d\n", V);    strcpy (S5, S6); printf ("%s,%s\n", S5, S6); strncpy (S1, S2,3);//Copy the first fewprintf"%s,%s\n", S1, S2); inti = $; Charss[ -]={0}; sprintf (SS,"i =%d", i);//output The content you want to output to a string? printf"%s\n", SS); //Ss=itoa (i);//is not a standard C-language library function. VS has//Atoi is a standard C-language library function        Chars7[ -]="abc=500"; Chars8[ -]="5+6="; SSCANF (S7,"abc=%d", &i);//use the string as input to output the specified variable to the specified content. printf"%d\n", i); intb; SSCANF (S8,"%d+%d=", &a, &b); printf ("%d,%d\n", A, b); Const Char*p = STRCHR (S7,'=');//Find charactersprintf"%s\n", p); P= Strstr (S7,"b");//Find Stringprintf"%s\n", p); strcpy (S7,"abc_123_ee_xx"); P=strtok (S7,"_");//Replace _ to 0//printf ("%s,%p,%p\n", p, p, S7); //P=strtok (NULL, "_");//How do you know which is the right operation???? //printf ("%s,%p,%p\n", p, p, S7); //P=strtok (NULL, "_");//How do you know which is the right operation???? //printf ("%s,%p,%p\n", p, p, S7);     while(p) {printf ("%s,%p,%p\n", p, p, S7); P=strtok (NULL,"_"); }    return 0;}

C String manipulation 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.