当前位置:网站首页>Character function and string function (I)
Character function and string function (I)
2022-07-26 04:37:00 【Yuan_ o_】
Preface
C The processing of characters and characters in language is very cumbersome , however C The language itself has no string type , Strings are usually placed in
Constant stringMedium orA character arrayin .String constantFor string functions that do not modify it
1、 Function introduction
1.1 strlen( Find the string length )
size_t strlen(const char * str);
- String to
'\0'As an end sign ,strlen Function returns in a string'\0'The number of characters that appear before ( It doesn't contain'\0'). - The string that the parameter only wants must be
'\0'end . - Note that the return value of the function is size_t, It's unsigned ( Fallible )
- strlen Simulation Implementation of function
notes :( Error model )
#include <stdio.h>
int main()
{
const char* str1 = "abcdef";
const char* str2 = "bbb";
if (strlen(str2) - strlen(str1) > 0)
// because strlen The return type of is an unsigned integer , So you can't compare string sizes by subtraction
// Correct demonstration :if(strlen(str2) > strlen(str1)
// perhaps if((int)strlen(str2) - (int)strlen(str1) > 0)
{
printf("str2>str1\n");
}
else
{
printf("str1>str2\n");
}
return 0;
}
Simulation Implementation strlen function :
#include <stdio.h>
#include <assert.h>
// Counter method
size_t my_strlen(const char* str)
{
size_t count = 0;
assert(str);
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
Unlimited length string function :
strcpy
strcat
strcmp
1.2 strcpy
char * strcpy ( char * destination, const char * source );
- Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
- 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 , To ensure that the source string can be stored
- The target space has to be variable
// Example 1
#include <stdio.h>
int main()
{
char name[20] = {
0 };
strcpy(name, "zhangsan");// Sure
//name = "zhangsan";//err,name The array name is the address , Address is a constant value , Cannot be assigned
char arr1[] = "word";
strcpy(name, arr1);// Sure
char arr2[] = {
'a','b','c' };
strcpy(name, arr2);//err, The source string does not '\0' This end sign , Will cause cross-border visits
printf("%s\n", name);
return 0;
}
// Example 2
int main()
{
const char* p = "abcdef";
char arr[] = "abc";
strcpy(p, arr);//err; The target area cannot be modified
return 0;
}
Simulation Implementation strcpy function :
// The first edition
#include <assert.h>
char* strcpy(char* dest, const char* src)
{
assert(dest);// Assertion , Judge whether the source function and the objective function are empty , Empty return 0, Don't go down
assert(src);
char* ret = dest;
while (*src != '\0')
{
*dest++ = *src++;
}
*dest = *src;// hold '\0' also copy To come over
return ret;
}
// The second edition ( Optimize First Edition )
#include <assert.h>
char* strcpy(char* dest, const char* src)
{
assert(dest&&src);// Assertion , Judge whether the source function and the objective function are empty , Empty return 0, Don't go down
char* ret = dest;
while (*dest++ = *src++)// Judgment , Again copy'\0'
{
;
}
return ret;
}
1.3 strcat
char * strcat ( char * destination, const char * source );
- Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
- The source string must be in
'\0'end . - The target space has to be large enough , It can hold the contents of the source string
- The target space must
- The string cannot be appended to itself ( Will be able to ’\0’ Cover ) Fall into a dead cycle
//strcat String append function example
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "hello ";
strcat(arr, "world");
printf("%s\n",arr);
return 0;
}
Simulation Implementation strcat function :
// Simulation Implementation strcat function
#include <stdio.h>
#include <assert.h>
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr[20] = "hello ";
my_strcat(arr, "world");
printf("%s\n",arr);
return 0;
}
1.4 strcmp
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, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
The standard stipulates :
- The first string is larger than the second string , Return greater than 0 The number of
- The first string is equal to the second string , Then return to 0
- The first string is less than the second string , Then return less than 0 The number of
// The wrong sample
#include <stdio.h>
int main()
{
char arr1[20] = "zhangsan";
char arr2[] = "zhangsanfeng";
// Compare whether the two strings are equal
//arr1 and arr2 It's an array name , Array name is the address of the first element of the array
// This compares the address of the first element of two strings , Definitely not equal
// So this comparison is wrong
if (arr1 > arr2)
{
printf(">\n");
}
else if (arr1 == arr2)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
// The correct sample
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[20] = "zhangsan";
char arr2[] = "zhangsanfeng";
int ret = strcmp(arr1, arr2);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
Simulation Implementation strcmp function :
// The first edition
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
if (*str1 > *str2)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
char arr1[20] = "zhangsan";
char arr2[] = "zhangsanfeng";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
// The second edition ( Optimize First Edition )
#include <stdio.h>
#include <assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1 - *str2;// This modification
}
int main()
{
char arr1[20] = "zhangsan";
char arr2[] = "zhangsanfeng";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
{
printf(">\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf("<\n");
}
return 0;
}
边栏推荐
- [uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
- Use of anonymous functions
- 九、文件上传和下载
- FFmpeg 视频编码
- Ffmpeg video coding
- NFT的几种发行方式你都了解过吗?不同的发行方式有什么优缺点?
- Yapi installation
- UE4 two ways to obtain player control
- Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
- MySQL usage
猜你喜欢

ASP. Net core actionfilter filter details

ES6 modularization +commonjs

Database startup message: ora-29702: error occurred in cluster group service

1. Mx6u system migration-6-uboot graphical configuration

Face database collection summary

理性认知教育机器人寓教于乐的辅助作用

Working principle and application of fast recovery diode

ES6模块化+CommonJS

一个sql server查询截止某个日期最新的记录

UE4 controls the rotation of objects by pressing keys
随机推荐
9、 File upload and download
Keil v5安装和使用
Codeforces Round #807 (Div. 2)
MySQL 执行失败的sql是否会计入慢查询?
一、基础入门
2022 Henan Mengxin League game (3): Henan University B - reverse pair count
Add watermark to ffmpeg video
Throttling anti shake function of JS handwritten function
建设面向青少年的创客教育实验室
青少年创客教育的创意设计原理
第三篇如何使用SourceTree提交代码
Phaser(一):平台跳跃收集游戏
2022河南萌新联赛第(三)场:河南大学 L - 合成游戏
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service
The difference between positive samples, negative samples, simple samples and difficult samples in deep learning (simple and easy to understand)
2022 Henan Mengxin League game (3): Henan University J - magic number
Database startup message: ora-29702: error occurred in cluster group service
Working principle and application of fast recovery diode
Weights & biases (II)