C C++ commonly used to manipulate strings of functions

  • 2020-05-30 20:47:38
  • OfStack

Just a little summary
The function name: stpcpy
Function: copy one string to another, stop at '\0', destin must have enough space
Usage: char *stpcpy(char *destin, char *source);

The function name: strcat
Power: string concatenation function, note that destin has plenty of space
Usage: char *strcat(char *destin, char *source);

The function name: strchr
Function: looking for the first match of a given character in a string, NULL cannot be found
Usage: char *strchr(char *str, char c);

The function name: strcmp
Power: string size comparison, str1 > str2 returns 1, str1 < str2 returns negative 1, equal returns 0
Usage: int strcmp(char *str1, char *str2);

The function name: strncmpi
Power: compare the first maxlen characters of the strings str1 and str2, ignoring case
Usage: int strncmpi(char *str1, char *str2, unsigned maxlen);

The function name: strcpy
Work: stop copying when '\0' is encountered. destin must have enough space
Usage: char *strcpy(char *str1, char *str2);

The function name: strcspn
Function: returns the value of the first character in the string s1 that appears in s2 in s1, that is, the length of the substring that appears in s1 but not in s2
Usage: int strcspn(char *str1, char *str2);
Application:
str1="Golden Global View"; str2 = "new"
In s1, and in s2, n e w is satisfied, while e is the first to appear in s1, so it returns its position

The function name: strdup
Power: copy the string to the new location. Note that when using this function, a new piece of memory is requested, so it must be freed after use
Usage: char *strdup(char *str);
Application:


#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
  char *dup_str, *string = "abcde";
  dup_str = strdup(string);	//  To apply for the 1 Slice the new memory address, dup_str It points to this piece of memory 
  printf("%s\n", dup_str);
  free(dup_str);		// 1 Need to release 
  return 0;
}

The function name: stricmp
Power: compare string sizes, ignoring case
Usage: int stricmp(char *str1, char *str2);

The function name: strerror
Power: returns a pointer to an error message string
Usage: char *strerror(int errnum);
Application:


#include <stdio.h>
#include <errno.h>
int main(void)
{
  char *buffer;
  buffer = strerror(errno);
  printf("Error: %s\n", buffer);
  return 0;
}

The function name: strcat0
Power: compare two strings, ignoring case
Usage: int strcmpi(char *str1, char *str2);

The function name: strncmp
Power: compare string size maxlen is the number of bits to compare
Use: int strncmp(char *str1, char *str2, int maxlen);

The function name: strncmpi
Function: compare the first part of a string to the first part of another string, case or case. maxlen is the number of bits to compare
Usage: int strncmpi(char *str1, char *str2,int maxlen);

The function name: strncpy
Power: string copy maxlen to specify how many bits to copy
Use: char *strncpy(char *destin, char *source, int maxlen);

The function name: strnicmp
Function: ignore case comparison string maxlen is the number of digits to compare
Use: int strnicmp(char *str1, char *str2, unsigned maxlen);

The function name: strnset
Function: sets the first n characters in a string to the specified character ch
Usage: char *strnset(char *str, char ch, unsigned n);

The function name: strpbrk
Function: find the position in the source string (s1) that first contains any of the characters in the search string (s2) and return, or if not, a null pointer
Usage: char *strpbrk(char *str1, char *str2);

The function name: strrchr
Function: looks for the last occurrence of a specified character in a string
Use: char *strrchr(char *str, char c);

The function name: strrev
Work: string inversion
Usage: char *strrev(char *str);

The function name: strset
Power: sets all characters in a string to the specified character
Use: char *strset(char *str, char c);

The function name: strspn
Function: returns the first subscript of the string str1 that does not appear in the specified string str2
Usage: int strspn(char *str1, char *str2);

The function name: strstr
Function: looks for the first occurrence of a specified string in a string
Usage: char *strstr(char *str1, char *str2);

The function name: strtok
Function: finds the word separated by the delimiter specified in the second string
Usage: char *strtok(char *str1, char *str2);
Application:


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
  char sentence[]="This is a sentence with 7 tokens";
  cout<< "The string to be tokenized is:\n"<< sentence <<"\n\nThe tokens are:\n\n";
  char *tokenPtr=strtok(sentence," ");
  while(tokenPtr!=NULL)
	{
    cout<<tokenPtr<<endl;
    tokenPtr=strtok(NULL," "); //  When called again, no 1 The parameters and NULL
  }
  
}

The function name: strupr
Function: converts lowercase letters in a string to uppercase letters
Usage: char *strupr(char *str);

The function name: swab
Power: bytes exchanged, nbytes is the number of bytes exchanged
Usage: void swab (char *from, char *to, int nbytes);
Application:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char source[15] = "Frank Borland";
char target[15];
int main(void)
{
  swab(source, target, strlen(source));
  printf("This is target: %s\n", target);
  return 0;
}

That's all, you can check out the following articles.


Related articles: