Basic C language series-string-related content

Source: Internet
Author: User
Tags strtok

1. char *

Char * buff = "orisun ";

Sizeof (buff) = 4 store a pointer with 32 bits

Strlen (buff) = 6 strlen returns the actual length of the string, excluding '\ 0' at the end'

2. Char [N]

Char Bu [100] = "orisun"; From 7th to 100th bits are both '\ 0'

Sizeof (BU) = 100 Bu is an array, sizeof returns the length of the array

Strlen (BU) = 6 strlen returns the actual length of the string, excluding '\ 0' at the end'

Note that before printf ("% s \ n", bu); Bu [100] = '\ 0 ';

3. Char []

Char Bu [] = "orisun"; first, Bu is an array. Its length is determined by the length of "orisun". sizeof (BU) = 7

We know that the length must be specified when an array appears for 1st times, because the compiler will allocate space for it during compilation, so the following statement cannot be compiled:

Char Bu [];

Bu = "orisun ";

4. Const char * cannot be assigned to char *

Const char * does not mean that the pointer cannot be modified, but the content pointed to by the pointer cannot be modified. Therefore, when a const char * is assigned to a char *, as a result, the purpose of adding the const is equivalent to a virtual one, which makes no sense at all.

5. You can assign const Char to Char.

The "value assignment" of the basic type is to copy a value. After the copy, no matter how you modify the subsequent char, it will not affect the original const char.

6. strcpy (char *, const char *)

Header file # include <string. h>

Define the function char * strcpy (char * DEST, const char * SRC );

Function Description strcpy () copies the SRC string to the address indicated by the Dest parameter.

Returns the start address of the string of the Dest parameter.

Additional instructions: if the memory space specified by the Dest parameter is not large enough, it may cause Bufferoverflow errors. Please pay special attention when writing programs, or replace it with strncpy.

7. strncpy

Define the function char * strncpy (char * DEST, const char * SRC, size_t N );

Function Description strncpy () copies the SRC string to the address specified by DeST.

8. Copy the strdup string

Define the function char * strdup (const char * s );

Function Description strdup () will first configure the same space size as the parameter S string with maolloc (), then copy the content of the parameter S string to the memory address, and then return the address. You can use free () to release the address.

Therefore, the difference between strdup and strcpy is that the strdup target pointer does not need or cannot be used to allocate space in advance, while strcpy allocates the space in advance.

Returns a string pointer to the copied string address. If the return value is null, the memory is insufficient.

9. strcat connection string (also known as strncat)

Char * strcat (char * DEST, const char * SRC)

Returns the first address of the DeST. the Dest must have enough space to accommodate the copied string.

10. strncasecmp (case-insensitive comparison string ignored)

Int strncasecmp (const char * S1, const char * S2, size_t N );

11. Split the strtok string

Char * strtok (char * s, char * delim );
Splits a string into a group of strings. S is the string to be decomposed, and delim is the separator string. Essentially, strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.

Char buff [] = "ABC $ SW $21 ";

Char * delim = "$ ";

Char * Title = strtok (buff, delim); // Title = "ABC"

Char * address = strtok (null, delim); // address = "SW"

Char * Area = strtok (null, delim); // area = "21"

Note: Char "Buff =" ABC $ SW $21 "cannot be used, because the strtok function changes the value of the first parameter at runtime. If you define a buff as a constant pointer, the value cannot be modified. When strtok is used, a segment error occurs.

12. sprintf (format string copy)

Char SQL [200];

Memset (SQL, 0,200 );

Sprintf (SQL, "insert into T1 (title, address, Area) values ('% s',' % s', '% s')", title, address, area );

1234567891011 #include<stdio.h>#include<string.h>int
*buff="10 0x1b abc3.14";    int
a,b;    float
c;    char
s[5];    sscanf(buff,"%d %x %3s%f",&a,&b,s,&c);    printf("%d  %d  %f  %s\n",a,b,c,s);    return

Output: 10 27 3.140000 ABC

13. strstr

Char * strstr (const char * haystack, const char * needle );

Locate the first position of the Child string needle from the haystack

14. strchr

Char * strchr (const char * s, int C );

Searches for the position where character C appears for the first time in string S.

Reverse query of strrchr

The functions and usage of strchr and index are the same.

The functions and usage of strrchr and rindex are the same.

15. strpbrk

Char * strpbrk (const char * s, const char * accept );

In S, locate the first occurrence location of any character in accept.

16. memchr

Void * memchr (const void * s, int C, size_t N );

Search for C in the first n Bytes of S. If C is found, the pointer pointing to C is returned. Otherwise, null is returned.

17. strspns

Size_t strspn (const char * s, const char * accept );

Returns the first character subscript in string s that does not appear in the specified string accept.

Size_t strcspn (const char * s, const char * reject );

Returns the subscript of the first character in the specified string reject in string S.

18. strsep

Char * strsep (char ** stringp, const char * delim );

Splits a string into a group of strings. Scan backward from the position pointed to by stringp. If a character pointing to a location by delim is encountered, replace this character with null and return the address pointed to by stringp .?
123456789101112 #include<stdio.h>#include<string.h> main(){    char
str[]="root:x::0:root:/root:/bin/bash:";    char
*token;    char
*buf;    buf=str;    while((token=strsep(&buf,":"))!=NULL){        printf("%s\n",token);    }}


rootx 0root/root/bin/bash

Note that Buf cannot be replaced with str in row 9th. during compilation, the expected parameter type is Char **, but the input is Char (*) [32]. A segment error occurs during running. STR is a char array, Buf is a char pointer, and the first row of BUF = STR is allowed, because the array name Buf stores the first address of the array, that is, the array name itself is also a pointer. It is also possible to assign a char (*) [10] to a char **, because they all point to pointers. But you want

Char * arr [] = {"ASD", "werr "};

Char * STR = "tgb ";

Char ** PTR = & STR;

Arr ++ can point to the next row of a two-dimensional array, while PTR ++ can only forward one byte

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: 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.