網站首頁 編程語言 正文
一、函數表
函數名 | 函數 | 功能 |
---|---|---|
strlen | size_t strlen(const char* s); | 返回字符串 s 的長度(不包括結尾的0) |
strcmp | int strcmp(const char* s1, const char* s2); | 比較兩個字符串,返回:如果 s1 == s2,返回 0;如果 s1 |
strcpy | char* strcpy(char* restrict dst, const char* reestrict src) | 把字符串 src 復制拷貝到字符串 dst,返回 dst,restrict 表明 src 和 dst 不能重疊 |
strcat | char* stract(char* restrict s1, const char* reestrict s2) | 把字符串 s2 拷貝到字符串 s1 的后面,連接成一個長的字符串,返回 s1 |
strchr | char* strchr(const char* s, int c) | 返回一個指針,指向字符串 s1 中字符 ch 的第一次出現的位置 |
strstr | char* strstr(const char* s1, const char* s2) | 返回一個指針,指向字符串 s1 中字符串 s2 的第一次出現的位置 |
二、strlen
實例
#include#include //自定義 strlen()函數 size_t mylen(const char* s) { int idx = 0; while (s[idx] != '\0') { idx++; } return idx; } int main() { char a[] = "Hello"; printf("mylen=%llu\n", mylen(a)); printf("strlen=%llu\n", strlen(a));// {'H', 'e', 'l', 'l', 'o'}; printf("sizeof=%llu\n", sizeof(a));// {'H', 'e', 'l', 'l', 'o','\0'}; return 0; }
運行結果
三、strcmp
實例
#include#include //自定義 strcmp()函數 int mycmp1(const char* s1, const char* s2) { int idx = 0; while (1) { if (s1[idx] != s2[idx]) { break; } else if (s1[idx] == '\0') { break; } idx++; } return s1[idx] - s2[idx]; } int mycmp2(const char* s1, const char* s2) { int idx = 0; while (s1[idx] == s2[idx] && s1[idx] != 0) { idx++; } return s1[idx] - s2[idx]; } int mycmp3(const char* s1, const char* s2) { int idx = 0; while (*s1 == *s2 && *s1 != 0) { s1++; s2++; } return *s1 - *s2; } int main() { char s1[] = "abc"; char s2[] = "abc"; char s3[] = "abc "; char s4[] = "bbc"; char s5[] = "Abc"; printf("mycmp1=%d\n", mycmp1(s1, s2)); printf("mycmp2=%d\n", mycmp2(s1, s3)); printf("mycmp3=%d\n", mycmp3(s1, s4)); printf("mycmp3=%d\n", mycmp3(s1, s5)); printf("strlen=%d\n", strcmp(s1, s2)); printf("strlen=%d\n", strcmp(s1, s3)); printf("strlen=%d\n", strcmp(s1, s4)); printf("strlen=%d\n", strcmp(s1, s5)); return 0; }
運行結果
VS編輯器的原因,庫函數 strlen 的結果只有 0, -1, 1 三種結果。
四、strcpy
// 復制一個字符串 char* dst = (char*)malloc(strlen(src)+1); strcpy(dst,src); // strlen 查 src 大小 +1 (+1 的原因是加上字符串最后的 ‘\0', strlen 讀取的長度不包括 ‘\0'), 再 malloc 分配空間給 dst, 這樣 dst 的空間正好等于 src, 然后再執行 strcpy 復制操作
實例
#include#include //自定義 strcpy()函數 char* mycpy1(char* dst, const char* src) { int idx = 0; while (src[idx] != '\0') { dst[idx] = src[idx]; idx++; } dst[idx] = '\0'; return dst; } char* mycpy2(char* dst, const char* src) { char* ret = dst; while (*src) { *dst++ = *src++; } *dst = '\0'; return ret; } int main() { char s1[] = "abc"; char s2[] = "def"; char s3[] = "ghi"; char s4[] = "jkl"; printf("s1=%s\n", s1); strcpy(s1, s2); printf("s1=%s\n", s1); mycpy1(s1, s3); printf("s1=%s\n", s1); mycpy2(s1, s4); printf("s1=%s\n", s1); return 0; }
運行結果
五、stract
實例
#include#include int main() { char s1[10] = "abc"; char s2[] = "def"; printf("s1=%s\n", s1); strcat(s1, s2); printf("s1=%s\n", s1); return 0; }
運行結果
六、strchr
實例
#include#include int main() { char s1[10] = "abcdebcde"; printf("%p\n", s1); printf("%p\n", s1 + 1); char* p = strchr(s1, 'b'); printf("%s\n", p); printf("%p\n", p); return 0; }
運行結果
總結
原文鏈接:https://blog.csdn.net/weixin_46398948/article/details/123647742
相關推薦
- 2022-06-08 兩步完成druid數據庫連接池的密文配置
- 2022-09-19 C語言數據結構之單鏈表存儲詳解_C 語言
- 2022-12-05 Android開發InputManagerService創建與啟動流程_Android
- 2022-07-03 如何讓Python在HTML中運行_python
- 2023-02-01 docker中安裝elasticsarch?等鏡像的過程_docker
- 2022-10-23 python?groupby函數實現分組選取最大值與最小值_python
- 2022-03-26 C++實現簡單猜數字小游戲_C 語言
- 2022-09-30 關于react中useCallback的用法_React
- 最近更新
-
- 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同步修改后的遠程分支