Linux string handling functions

1 strchr

function prototype: extern char * strchr (char * str, char character)

Parameters: str is a pointer to a string, character as a character to be searched.
        
Where the library name: #include <string.h>
 
Function: location-finding character from the character string str first appears.
 
Return Description: Returns a pointer to the first character position of the character appears once, if not found it returns NULL.

2 strstr Function

Function: to find a string in a string

function prototype: char * strstr (char const * s1, char const * s2);

Function Description: Strstr realize the search locations throughout s2 s1 in the first occurrence of the excluding str2 string terminator, and returns a pointer to that location. If s2 does not appear anywhere in the full s1, the function returns a NULL pointer. If the second string is a null string, the function returns s1.

     The difference between the two functions, is to find the strchr single character in the string may be '\ n' newline (verified), Strstr is to find the position in s1 s2 appears in lookup a string for string where it appears.

3 strcat function

prototype: char * strcat (char * dest , char * src)
Parameters: dest is a pointer to a string object, i.e. the string concatenation (front), a source of the src pointer string (in Rear).
       
Where the library name: #include <string.h>
 
Function: Add the indicated string src to the end of the connection string dest achieved, cover the connection process at the end of dest '/ 0'.
 
Description Returns: memory pointed to src and dest region may not overlap, and dest must have sufficient space to accommodate the return string src pointer pointing dest

4 strncat Function

Function Prototype: extern char * strncat (char * dest, char * src , int n)
parameters: source string src, dest purpose string, n specified in src first n characters.
       
Where the library name: #include <string.h>
 
Function: the first n characters referred src string added to the end of dest, covered at the end of dest '/ 0', achieve string concatenation.
 
Description Returns: Returns a pointer, the concatenated string.

 

5 strlen Function

Function: size_t strlen (const char * s );

Function: strlen () is used to calculate the length of a specified string s, not including the character "\ 0."

Header: #include <string.h>

Return Value: returns the number of characters in the string s.

 

6 strcmp function

function: int strcmp (const char * s1 , const char * s2);

Function: strcmp () to compare the parameters s1 and s2 string. Compare the size of the string is on the order of ASCII code table to decide the value of this order is also a character. strcmp () is first subtracted value s1 s2 first character of the first character value, if the difference is 0 to continue the next character comparison, if the difference is not 0 if the difference is returned. For example the string "Ac" and "ba" is returned comparison character "A" (65) and 'b' (98) a difference (-33).

Header: #include <string.h>

Return Value: If the same parameters s1 and s2 0 string is returned. If s1 greater than s2 return value greater than zero. If less than s1 s2 value less than 0 is returned.

 

7 strcpy function

functions: char * strcpy (char * dest , const char * src);

Function: strcpy () will copy the parameters to the parameter string src dest indicated address.

Header: #include <string.h>

Return Value: returns a string starting address parameter of dest. If dest parameter within the meaning of the memory space is not big enough, may result in an error condition buffer overflow (buffer Overflow), please pay special attention in the preparation of the program, or use strncpy () instead.

8 strncpy Function

Function: char * strncpy (char * dest , const char * src, size_t n);

Function: Copies string src contents (characters, numbers, characters ....) to the string dest, the value is determined by the number n of replication. If the first n characters of src does not contain a NULL character, the result will not end with a NULL character. If n <src is, simply copy the first n characters of src to the first n characters of dest, does not automatically add '\ 0', i.e. the result dest not include '\ 0', need to manually add a '\ 0 '. If the length is less than n bytes of src, dest filling places NULL until n bytes have been copied. memory pointed to src and dest dest region may not overlap and must have sufficient space to accommodate the length of src + character '\ 0'

header file: #include <string.h>

Parameter Description target string pointer] dest, src the source string pointer. strncpy () src string will be copied to the first n characters string dest. src and dest referred memory area do not overlap, and dest must have sufficient space n characters.

Unlike strcpy (), strncpy () does not mark the end of dest append '\ 0', so the use of strcat (after) the strncpy () could not find '\ 0' be a problem;

if you want to strncpy () at the end Add "\ 0" src need greater than the string length n

9 strpbrk function

function: char * strpbrk (const char * str, const char * group);

function: sequentially checks the character string str, when being tested in the character when the group is also included in the string, then the test stops, and returns the character position, NULL null characters are not included.

Header file: #include <string.h>





Function: char * strtok (char * s , const char * delim);

functions: resolving string is a set of strings. s is the string to be decomposed, delim a delimiter string, strtok () for dividing a character string into segments. S point parameter string to be divided, the parameter string delim was divided, when strtok () will be changed to the character to character segmentation parameter is found in the string delim the parameter s \ 0 characters. In the first call, strtok () Required parameter s given string, subsequent calls will be set to the parameter s NULL. Each call is successfully divided segment pointer is returned.

Header: #include <string.h>

Return Value: s from the start of a number of divided string. When the string is not divided NULL is returned. Delim contains all characters are filtered out, and will be filtered out is a node where a divided

Example:

Use the separator character string in the second parameter ct, S dividing the first parameter, the parameters ct delimiter can be any character, the character may be a single separator may be a separator in the form of a string, such as: "!,; '/" and the like, can be used as the separator. For example:

S = "! ABC, DEF, 123; 456 / AAA"
CT = "!,; /"
S is divided as: abc def 123 456 aaa

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char s[] = "abc,def,123;456!/aaa";
  char delim[] = " ,;!/";

  char *p = NULL;
  for(p = strtok(s, delim); p != NULL; p = strtok(NULL, delim))
  {
    printf("%s ", p);
  }
  printf("\n");

  return 0;
}

 



strtok.c -o strtok gcc
$ ./strtok
abc DEF 123 456 aaa
---------------------
Author: xiaoyao1004
Source: CSDN
Original: https: // blog .csdn.net / xiaoyao1004 / article / details / 83863344
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/liujx2019/p/11248398.html