網站首頁 編程語言 正文
一、strlen()
1. 函數原型:
size_t strlen(const char *str)
參數str: 要計算的字符串的長度
返回值: 返回字符串 str 的長度,直到遇到結束字符'\0',但不包括結束字符。
size_t 是一個無符號整數類型。
2. 手動實現:
源代碼:
#include <stdio.h>
#include <string.h>
size_t my_strlen(const char *str)
{
size_t len = 0;
while (*str++ != '\0') {
len++;
}
return len;
}
int main()
{
char *p = "hello world";
int len_1 = 0, len_2 = 0;
len_1 = my_strlen(p);
len_2 = strlen(p);
printf("len_1 = %d, len_2 = %d\n", len_1, len_2);
return 0;
}
運行結果:
len_1 = 11, len_2 = 11
二、strcat()
1. 函數原型:
char *strcat(char *dest, const char *src)
參數1: 目標字符串,該空間需要足夠容納后續追加的字符串。
參數2: 要追加的字符串。
返回值: 返回指向目標字符串的指針。
2. 手動實現:
源代碼:
#include <stdio.h>
#include <string.h>
char *my_strcat(char *dest, const char *src)
{
while (*dest != '\0') {
dest++;
}
while (*src != '\0') {
*dest++ = *src++;
}
*dest = '\0';
return dest;
}
int main()
{
char p1[20] = "hello ";
char p2[20] = "hello ";
char *q = "world";
my_strcat(p1, q);
strcat(p2, q);
printf("p1 = %s, p2 = %s\n", p1, p2);
return 0;
}
運行結果:
p1 = hello world, p2 = hello world
三、strcpy()
1. 函數原型:
char *strcpy(char *dest, const char *src)
參數1: 要存儲的目標字符串。
參數2: 要復制的字符串。
返回值: 返回指向目標字符串的指針。
2. 手動實現:
源代碼:
#include <stdio.h>
#include <string.h>
char *my_strcpy(char *dest, const char *src)
{
while (*src != '\0') {
*dest++ = *src++;
}
*dest = '\0';
return dest;
}
int main()
{
char p1[20] = {'\0'};
char p2[20] = {'\0'};
char *q = "hello world";
my_strcpy(p1, q);
strcpy(p2, q);
printf("p1 = %s, p2 = %s\n", p1, p2);
return 0;
}
運行結果:
p1 = hello world, p2 = hello world
四、strcmp()
1. 函數原型:
int strcmp(const char *str1, const char *str2)
參數1: 要比較的第一個字符串
參數2: 要比較的第二個字符串
返回值:
?? ?如果返回值 < 0,則表示 str1 小于 str2。
?? ?如果返回值 > 0,則表示 str1 大于 str2。
?? ?如果返回值 = 0,則表示 str1 等于 str2。
2. 手動實現:
源代碼:
#include <stdio.h>
#include <string.h>
int my_strcmp(const char *str1, const char *str2)
{
int ret = 0;
while ((*str1 != '\0') && (*str1 == *str2)) {
str1++;
str2++;
}
ret = *str1 - *str2;
return ret;
}
int main()
{
char p1[20] = {"hello world"};
char p2[20] = {"hello aorld"};
int ret_1, ret_2;
ret_1 = my_strcmp(p2, p1);
ret_2 = strcmp(p2, p1);
printf("ret_1 = %d, ret_2 = %d\n", ret_1, ret_2);
return 0;
}
運行結果:
ret_1 = -22, ret_2 = -22
五、memset()
1. 函數原型:
void *memset(void *str, int c, size_t n)
參數1: 要設置的內存塊
參數2: 要填充的值,在填充內存塊時是使用該值的無符號字符形式。
參數3: 填充的字符個數。
返回值: 返回指向str的指針。
2. 手動實現:
源代碼:
#include <stdio.h>
#include <string.h>
void *my_memset(void *str, int c, size_t n)
{
if (str == NULL)
return NULL;
char *pstr = (char*)str;
while (n-- > 0) {
*pstr++ = c ;
}
return str;
}
int main()
{
char p1[20] = {"hello world"};
char p2[20] = {"hello world"};
my_memset(p1, '*', 5);
memset(p2, '*', 5);
printf("p1 = %s, p2 = %s\n", p1, p2);
return 0;
}
運行結果:
p1 = ***** world, p2 = ***** world
原文鏈接:https://blog.csdn.net/liung_/article/details/124236693
相關推薦
- 2022-10-07 C#如何實現調取釘釘考勤接口的功能_C#教程
- 2024-07-18 MybatisPlus優雅實現加密?
- 2022-04-18 C#實現在窗體上的統計圖效果_C#教程
- 2022-10-23 C++進程的創建和進程ID標識詳細介紹_C 語言
- 2022-05-31 在.NET?MAUI應用中配置應用生命周期事件_實用技巧
- 2023-05-05 Python?pip更新的兩種方式詳解_python
- 2022-07-24 .Net結構型設計模式之代理模式(Proxy)_基礎應用
- 2022-06-01 利用Python實現外觀數列求解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支