当前位置:网站首页>Character function, string function and memory function of C language
Character function, string function and memory function of C language
2022-07-27 16:55:00 【Cabbage 00】
Catalog
String function with unlimited length
Length restricted string function
Character classification function
Other character classification functions ( similar )
Preface
C The processing of characters and strings in languages is frequent , but C The language itself has no string type , Strings are usually placed in constant strings , Or string array , String constants apply to string functions that do not modify them
String function with unlimited length
strlen function


Be careful :size_t The meaning of shaping for unsigned
Use :
Import header file :include <string.h>
#include <stdio.h>
#include <string.h>
void main() {
char c[] = "hello";// String is different from character array , There is..., by default '\0';
char d[] = { 'h','e','l','l','o' };
printf("%d\n", strlen(c));//5
printf("%d\n", strlen(d));// Random value
}strcpy function

- take source Copy string to destination On (destination If it is not an empty string, it will be source String override ), The return value is destination The first address
- The source string must be in '\0' end
- The of the source string will be '\0' Copy to target string
- The target string space must be large enough , To ensure that the source string can be stored
- The target space has to be variable
- Learn to simulate
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[20] = { 0 };
char arr2[] = "hello world";
strcpy(arr1, arr2);// The return value is arr1 The first address
printf("%s", arr1);
}strcat function

- Append the source string data to the target string , The return value is destination The first address
- The source string must be in '\0' end
- The target space must be large enough , Can accommodate the contents of the source string
- The target space must be modifiable
- You can't add to yourself ( reason :'\0' Being replaced cannot pause the loop )
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[20]="hello ";
strcat(arr1, "world");// Append string , The return value is the first address of the target string
printf("%s\n", arr1);//hello world
}strcmp function

Comparison of two strings
- == Compare , The comparison is 2 Address value of a string
- strcmp(str1,str2) Function to compare whether the contents of two strings are equal , If the contents of two strings are equal , Then this function will return 0
The return value of this function is as follows :
- If the return value is less than 0, said str1 Less than str2.
- If the return value is greater than 0, said str1 Greater than str2.
- If the return value is equal to 0, said str1 be equal to str2.
Comparison method : Compare ASCII Code values are compared one by one from beginning to end when they appear for the first time ASCII Different code values , Then this character ASCII The string where the code big character is located is larger than another string .
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[] = "abc";
char arr2[] = "abc";
if (0==strcmp(arr1,arr2))
{
printf(" The contents of two strings are equal ");
}
else if(0<strcmp(arr1,arr2))
{
printf("arr1 Greater than arr2");
}
else
{
printf("arr1 Less than arr2");
}
}Length restricted string function
strncpy function
![]()
- Before the source string num Characters are copied into the target string , The return value is the first address of the target string
- If the number of copies of the original array exceeds the length of the source string , Then the subsequent copy uses '\0' Add
- The target space must be large enough , Can accommodate the contents of the source string
- The target space must be modifiable
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[20] = "hello";
char arr2[] = "hi world";
printf("%s\n", strncpy(arr1, arr2,2));//hillo
}strncat function
![]()
- Before the source string num Characters are appended to the target string , The return value is the first address of the target string
- If the number of copies of the original array exceeds the length of the source string , Only append to the source string length
- You can add to yourself
- The target space must be large enough , Can accommodate the contents of the source string
- The target space must be modifiable
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[20] = "hello ";
char arr2[] = "world hahaha";
printf("%s\n", strncat(arr1, arr2,5));//hello world
}strncmp function

- take str1 String and str2 String comparison num Characters , The return value type is the same as strcmp
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char* p = "abcd";
char* q = "abed";
int ret = strncmp(p, q, 2);// Compare 2 Characters
int res = strncmp(p, q, 3);// Compare 3 Characters
printf("%d\n", ret);//0
printf("%d\n", res);//-1
}Other string functions
strstr function

- stay str1 Find whether the string contains str2 character string , If it exists, the string at the first occurrence position is returned , If it doesn't exist , Is returned null, If str2 If an empty string is passed, it returns str1
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[] = "abcdefabcdef";
char arr2[] = "bcd";
// stay arr1 Find out whether it contains arr2
printf("%s\n", strstr(arr1, arr2));//bcdefabcdef
}strtok function

- seq It's a string , Defines the set of characters used as separators
- The first parameter specifies a string , It contains 0 One or more by seq A mark separated by one or more separators in a string .
- strtok Function found str The next mark in , And use it ’\0' Replace end , Returns a pointer to the tag .( Be careful :strtok Function changes the string being manipulated , So it's using strtok The string segmented by the function is generally a temporary copy of the content , And modifiable ).
- strtok The first argument of the function is not null, Function will find str The first mark in ,strtok The function will save its position in the string
- strtok The first argument to the function is null, The function will start at the same position in the string that is saved , Find next tag
- If there are no more tags in the string , Then return to null The pointer
Use :
Import header file :include <string.h>
#include <stdio.h>
#include<string.h>
void main() {
char arr1[] = "abcdefabcdef";
char tmp[30] = { 0 };
strcpy(tmp, arr1);// Temporary copy
char arr2[] = "cf";// Set of delimiters
char* ret = NULL;
for (ret = strtok(tmp, arr2); ret != NULL; ret = strtok(NULL, arr2)) {
printf("%s\n", ret);
}
}strerror function

- The error code will be set when calling the library function fails ,errnum Indicates the error code , Pass the error code to strerror Function , This function will return the first address of the error message string corresponding to the error code .( Translate the error code into error message )
Use :
Import header file :include <string.h>、include <errno.h>
#include <stdio.h>
#include<string.h>
void main() {
printf("%s\n", strerror(0));//No error
printf("%s\n", strerror(1));//Operation not permitted
printf("%s\n", strerror(2));//No such file or directory
printf("%s\n", strerror(3));//No such process
printf("%s\n", strerror(4));//Interrupted function call
}Be careful :errno For in C Global error code in language , To use, you must import header files include <errno.h>
#include <stdio.h>
#include<string.h>
#include <errno.h>
int main()
{
FILE* pFile;
pFile = fopen("test.txt", "r");
if (pFile == NULL)
printf("Error opening file test.txt: %s\n", strerror(errno));
// When fopen There will be an error code when the library function call fails , Then put the error code in the back errno In this variable , then strerror The function will be based on the error code , Print out the corresponding error message
return 0;
}Character classification function
isdigit function
![]()
Judge whether the function is a character function
- The parameters inside pass characters ASCII Code value , If the character is a numeric character , Then return a non-zero value , If the character is not a numeric character , Then return to 0;
Use :
Import header file :include <ctype.h>
#include <stdio.h>
#include <ctype.h>
void main() {
char c = '6';// Numeric character
char d = '@';// Nonnumeric character
int ret = isdigit(c);
int res = isdigit(d);
printf("c=%d,d=%d", ret,res);//c=4,d=0
}islower function
Judge whether a character is lowercase
- The parameters inside pass characters ASCII Code value , If the character is lowercase , Then return a non-zero value , If the character is not lowercase , Then return to 0;
Use :
Import header file :include <ctype.h>
#include <stdio.h>
#include <ctype.h>
void main() {
char c = 'A';// Uppercase characters
char d = 'a';// Lowercase characters
int ret = islower(c);
int res = islower(d);
printf("c=%d,d=%d", ret,res);//c=0,d=2
}Other character classification functions ( similar )

Character conversion function
tolower function
Character to lowercase
- The parameters in it are in uppercase characters ASCII Code value , This function will then convert the upper case character to the lower case character ASCII The code value returns .
Use :
Import header file :include <ctype.h>
#include <stdio.h>
#include <ctype.h>
void main() {
char arr[20] = "HELLOWORLD";
int i = 0;
while (arr[i]!='\0')
{
if (isupper(arr[i])) {
arr[i] = tolower(arr[i]);
printf("%c", arr[i]);
}
i++;
}
}
//helloworldBe careful :toupper Functions are similar to this ( Capitalize characters )
Memory function
memcpy function
![]()
Memory copy function
- hold source The data pointed to by space is copied to destination Go inside the space , Inside num The parameter indicates how much space to copy ( In bytes ), The return value is destination The first address
- memcpy Functions can only copy non overlapping memory
Use :
Import header file :include <string.h>
#include <stdio.h>
#include <string.h>
void main() {
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
memcpy(arr2, arr1, 20);// take arr1 Inside 20 byte (5 Elements ) copy to arr2 in
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d ", arr2[i]);
}
//1 2 3 4 5 0 0 0 0 0
}#include <stdio.h>
#include <string.h>
void main() {
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
int* p=(memcpy(arr2, arr1, 20));// take arr1 Inside 20 byte (5 Elements ) copy to arr2 in
printf("%d ", *p);//1
}memmove function
![]()
Memory move function
- hold source The spatially oriented data is moved to destination Go inside the space , Inside num The parameter indicates how much space to move ( In bytes ), The return value is destination The first address
- memmove Function can handle overlapping memory
Use :
Import header file :include <string.h>
#include <stdio.h>
#include <string.h>
void main() {
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
memmove(arr1+2, arr1, 20);// take arr1 Inside 20 byte (5 Elements ) Move to arr1+2 in
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d ", arr1[i]);
}
//1 2 1 2 3 4 5 8 9 10, use memcpy Can't achieve
}memcmp function
![]()
Memory compare function
- ptr1 And in memory ptr2 Front in memory num If the bytes are the same, then it returns 0, If ptr1 Data in memory and ptr2 The data in memory is different ( front num Within bytes ) In different data , if ptr1>ptr2 Return greater than 0 Number of numbers , if ptr1<ptr2 Then return less than 0 Number of numbers .
Use :
Import header file :include <string.h>
#include <stdio.h>
#include <string.h>
void main() {
float arr1[] = { 1.0,2.0,3.0,4.0 };
float arr2[] = { 1.0,3.0};
int ret = memcmp(arr1, arr2, 4);// Compare the former 4 Bytes
int res = memcmp(arr1, arr2, 8);// Compare the former 8 Bytes
printf("ret=%d,res=%d\n", ret, res);//ret=0,res=-1
}memset function
![]()
Memory setting function ( Set memory in bytes )
- hold ptr The front of the memory pointed to num” byte “ All the contents of are set to what we want value value , return ptr The first address of the memory pointed to
- Integer array settings cannot be used memset
Use :
Import header file :include <string.h>
#include <stdio.h>
#include <string.h>
void main() {
char arr[] = "hello world";
char* c=memset(arr, 'x', 5);// take arr An array of former 5 Bytes changed to x
printf("%s\n", c);//xxxxx world
}边栏推荐
- Opencv (IV) -- image features and target detection
- 被动收入:回归原始且安全的两种赚取方法
- Gurobi——GRBModel
- Bean: the difference between model and entity
- Opencv (I) -- basic knowledge of image
- Log4j.jar and slf4-log4 download link
- kubesphere多节点安装出错
- Matlab legend usage
- Codeforces Round #100 E. New Year Garland & 2021 CCPC Subpermutation
- T5 learning
猜你喜欢

OpenCV(二)——图像基本处理

【论文阅读】Single- and Cross-Modality Near Duplicate Image PairsDetection via Spatial Transformer Compar

Getting started with nvida CUDA dirverapi

OpenCV(三)——图像分割

jsp-El表达式,JSTL标签

MPC_ ORCA

Mazak handwheel maintenance Mazak little giant CNC machine tool handle operator maintenance av-eahs-382-1

MPC5744p时钟模块

OpenCV(五)——运动目标识别
![[paper reading] transformer with transfer CNN for remote sensing imageobject detection](/img/a2/8ee85e81133326afd86648d9594216.png)
[paper reading] transformer with transfer CNN for remote sensing imageobject detection
随机推荐
Database notes sorting
Kubesphere multi node installation error
Segment tree beats~
【论文阅读】A CNN-Transformer Hybrid Approach for CropClassification Using MultitemporalMultisensor Images
The difference between MVC and MVP and MVVM
LNMP environment - deploy WordPress
Apache
Configuration and application of gurobi in pycharm
Cubemx combined with IAR engineering transplantation
CODIS cluster deployment
android中的图片三级缓存
C language output string in reverse order
[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images
Mazak handwheel maintenance Mazak little giant CNC machine tool handle operator maintenance av-eahs-382-1
Servlet uses cookies to realize the last login time of users
MQ Series 2: technology selection of Message Oriented Middleware
Random number formula random
MPC5744p时钟模块
training on multiple GPUs pytorch
Process control statement