当前位置:网站首页>Character functions and string functions
Character functions and string functions
2022-07-20 08:30:00 【Feverish CPU】
Catalog
Character classification function ( Need to reference header file #include)
C The language itself has no string type , Strings are stored in constant strings or character arrays .
strlen
Find the string length
size_t strlen ( const char * str );
strlen Function returns in a string '\0' The number of characters that appear before ( It doesn't contain '\0')
The string pointed to by the formal parameter must be in '\0' end
The return value of the function is size_t, yes unsigned int Unsigned integer
Simulation Implementation :
Use the counter to simulate strlen
#include<assert.h>
size_t our_strlen(const char* p)
{
int count = 0;
assert(p != NULL);
while (*p!='\0')
{
*p++;
count++;
}
return count;
}
int main()
{
char* str = "Computer is very interesting!";
unsigned int ret = our_strlen(str);
printf("%d\n", ret);
return 0;
}
Using function recursion strlen
size_t our_strlen(const char* p)
{
if (*p == '\0')
return 0;
else
return our_strlen(p+1) + 1;
}
int main()
{
char* str = "Computer is very interesting!";
unsigned int ret = our_strlen(str);
printf("%d\n", ret);
return 0;
}
With a pointer - Pointer implementation strlen
size_t our_strlen(const char* p)
{
char* init_p = p;
while (*p != '\0')
{
p++;
}
return p - init_p;
}
int main()
{
char* str = "Computer is very interesting!";
unsigned int ret = our_strlen(str);
printf("%d\n", ret);
return 0;
}
( Specifically, the pointer - The pointer : When two pointers point to elements in the same array , Only one pointer is allowed to subtract another pointer , The result type of subtracting two pointers is a signed integer , The value of subtraction is the distance between two pointers in memory ( The distance is measured in the number of cells spaced, not bytes )
strcpy
String copy
char * strcpy ( char * destination, const char * source );
The source string must be in '\0' end
In the source string '\0' Copy to target space
The target space has to be large enough
The target space has to be variable
Simulation Implementation :
#include<assert.h>
char* our_strcpy(char* destination, char* source)
{
char* ret = destination;
assert(destination && source);
while (*source)
{
*destination = *source;
*destination++;
*source++;
}
*destination = *source;
return ret;
}
int main()
{
char a[50] = { 0 };
char* b = "I want to get so much money.";
our_strcpy(a, b);
printf("%s\n", a);
return 0;
}
strcat
String append
char * strcat ( char * destination, const char * source );
The source string must be in '\0' end
The target space has to be large enough
The target space must be modifiable
The string appends itself -> Dead cycle
Simulation Implementation :
#include<assert.h>
char* our_strcat(char* destination, const char* source)
{
assert(destination && source);
char* ret = destination;
while (*destination)
{
destination++;
}
while (*destination++ = *source++)
{
;
}
return ret;
}
int main()
{
char a[50] = "I must get ";
char* b = "so much money.";
our_strcat(a, b);
printf("%s\n", a);
return 0;
}
strcmp
The two strings are equal ( Compare content )
int strcmp ( const char * str1, const char * str2 );
This function starts comparing the first character of each string . If they are equal to each other , Then continue to use the following pair , Until the characters are different or reach the terminating null character . The comparison is ACSII Code size .
Simulation Implementation :
#include<assert.h>
int our_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1++==*str2++)
{
if (*str1 == '\0')
return 0;
}
return *str1 - *str2;
}
int main()
{
char a[] = "abc";
char b[] = "abd";
int ret = our_strcmp(a, b);
if (ret > 0)
printf(">\n");
else if (ret == 0)
printf("<\n");
else
printf("==\n");
return 0;
}
strncpy
String copy ( The length is limited )
char * strncpy ( char * destination, const char * source, size_t num );
Copy num Characters from the source string to the target space
If the source string length is less than num, After copying the source string , Add... After the target 0
Application, for example, :
#include<string.h>
int main()
{
char a[20] = "abcdefghigk";
char b[] = "xyz";
strncpy(a, b, 2);
printf("%s\n", a);
return 0;
}
strncat
String append ( The length is limited )
char * strncat ( char * destination, const char * source, size_t num );
Application, for example, :
#include<string.h>
int main()
{
char a[80];
char b[30];
strcpy(a, "I want to get so much ");
strcpy(b, "money.gogogo");
strncat(a, b, 6);
printf("%s\n", a);
return 0;
}
strncmp
The two strings are equal ( The length is limited )
int strncmp ( const char * str1, const char * str2, size_t num );
num Compare all the characters
Application, for example, :
#include<stdio.h>
#include<string.h>
int main()
{
char str[][5] = { "nbDS","yyds","yyss"};
puts("Looking for R2 astromech droids...");
for (int i = 0; i < 3; i++)
{
if (strncmp(str[i], "yyxx", 2) == 0)
printf("%s\n", *(str + i));
}
return 0;
}
strstr
A function to find a substring
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
Application, for example, :
int main()
{
char str[] = "I want to get so much money.";
char* p;
p = strstr(str, "money");
strncpy(p, "happy hahaha", 5);
puts(str);
return 0;
}
Simulation Implementation :
#include<assert.h>
char* our_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* p1 = str1;
const char* p2 = str2;
const char* p = str1;
while (*p)
{
p1 = p;
p2 = str2;
while (*p1 != '\0' && p2 != '\0' && *p1 == *p2)
{
p1++;
p2++;
}
if (*p2 == '\0')
{
return (char*)p;
}
p++;
}
return NULL;
}
int main()
{
char str1[] = "The money is so difficult to get.";
char str2 = "difficult";
char* ret = our_strstr(str1, str2);
if (ret == NULL)
printf(" Substring does not exist \n");
else
printf("%s\n", ret);
return 0;
}
strtok
Cut string
char * strtok ( char * str, const char * delimiters );
delimiters The parameter is a string , Defines the set of characters used as separators
The first parameter specifies a string , It contains 0 One or more by delimiters A mark separated by one or more separators in a string
strtok Function found str The next mark in , And use it '\0' ending , Returns a pointer to the tag
strtok The first argument of the function is not NULL, Function will find str The first mark in ,strtok Function will hold 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
Application, for example, :
int main()
{
char str[] = "-This is,a sample string.";
char* pch;
printf("Splitting string \"%s\"into tokens:\n", str);
pch = strtok(str, ",.-");
while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, ",.-");
}
return 0;
}
strerror
Return the error message corresponding to the error code
char * strerror ( int errnum );
Application, for example, :
#include <errno.h>// The header file that must be included
int main()
{
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL)
printf("Error opening file unexist.ent: %s\n", strerror(errno));
//errno: Last error number
return 0;
}
Character classification function ( Need to reference header file #include<ctype.h>)
function | If his parameters meet the following conditions, it returns true |
iscntrl | Any control character |
isspace | Blank character : Space ‘ ’, Change the page ‘\f’, Line break '\n', enter ‘\r’, tabs '\t' Or vertical tabs '\v' |
isdigit | Decimal number 0~9 |
isxdigit | Hexadecimal number , Include all decimal digits , Lowercase letters a~f, Capital A~F |
islower | Lowercase letters a~z |
isupper | Capital A~Z |
isalpha | Letter a~z or A~Z |
isalnum | Letters or numbers ,a~z,A~Z,0~9 |
ispunct | Punctuation , Any graphic character that is not a number or letter ( Printable ) |
isgraph | Any graphic character |
isprint | Any printable character , Including graphic characters and white space characters |
Character case conversion
tolower
int tolower ( int c );
toupper
int toupper ( int c );
mencpy
Memory copy ( Copy data in two separate spaces )
void * memcpy ( void * destination, const void * source, size_t num );
function mencpy from source The position of begins to be copied back num Bytes of data to destination Memory location for
This function is encountering '\0' It doesn't stop
If source and destination There is any overlap , The results of replication are undefined
Simulation Implementation :
void* memcpy(void* dst, const void* src, size_t count)
{
void* ret = dst;
assert(dst);
assert(src);
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return(ret);
}
memmove
Memory copy
void * memmove ( void * destination, const void * source, size_t num );
and memcpy The difference is that memmove The source and target memory blocks processed by the function can overlap
If the source space and the target space overlap , It should be used memmove Function processing
Simulation Implementation :
void* memmove(void* dst, const void* src, size_t count)
{
void* ret = dst;
if (dst <= src || (char*)dst >= ((char*)src + count)) {
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else {
dst = (char*)dst + count - 1;
src = (char*)src + count - 1;
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return(ret);
}
memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
take ptr1 Before the memory block pointed to num Bytes and ptr2 The first byte pointed to Count Compare , If they all match , Then return to zero. , Or if it doesn't match , Then return a value different from zero , Indicates which value is greater .
This is the end of sharing , I feel that the following string is becoming more and more complex ~ Purring
边栏推荐
- 目标检测划分数据集
- pytorch 目标检测 coco API 讲解 数据生成
- Pytorch target detection coco API explanation data generation
- Pytorch target detection data processing (II) extracting difficult samples, low AP samples
- Pytorch uses free GPU test training (aistudio) yolov4 as an example
- C language structure type
- C语言中动态内存的开辟
- ZABBIX agent adds a user-defined monitoring item -- Ping to destination IP link monitoring
- C语言中的文件操作
- Esp32 -- implement email sending function
猜你喜欢
随机推荐
The longest common subsequence of order 2 and order n
字符函数和字符串函数
STM32-点灯程序
yolov3的权重文件和预训练文件
指针数组和数组指针有什么区别?
枚举(enum)奇妙的使用、联合体(共用体union)对空间节省的巧妙
自定义类型:结构体,位段,枚举,联合
三子棋游戏
用C实现三种版本的通讯录
基数排序(桶排序)
Pytorch target detection coco API explanation data generation
YOLOv5苹果香蕉检测
Path in sword finger offer matrix
Pytorch mmdetection2.0 installation training test (coco training set)
What if the game needs to be reinstalled after the steam folder is moved
ListView的item展开后完整显示
C language structure type
第七十四篇:机器学习优化方法及超参数设置综述
YOLOv5实现吸烟行为检测
C语言程序环境和预处理