網站首頁 編程語言 正文
1.C語音的字符串有兩種
1.1字符數組
數組可以修改其中某一個值,不可以整體賦值。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <Windows.h> //使用字符數組存儲字符串 void main() { //三種寫法 //\0代表 結束符 char str[] = { 'h','e','l','l','o','\0' }; //char str[6]= { 'h','e','l','l','o' }; //char str[10] = "hello"; printf("%s\n", str); //地址 printf("%#x\n", str); getchar(); }
結果輸出:
hello
0xb3fb78
1.2字符指針
字符指針不可以修改其中某一個值,可以整體賦值。使用指針加法,結合結束符,可以進行截取。
void main() { char *str = "how are you?"; printf("%s\n", str); //str[1] = "w" ; //字符指針不可以修改其中某一個值 str = "hello world"; printf("%s\n", str); printf("%#x\n", str); //使用指針加法,截取字符串 str += 3; //指向第四個字符首地址 while (*str) { printf("%c", *str); str++; } getchar(); }
結果輸出:
how are you?
hello world
0x97b44
lo world
2.字符串常用的方法
相關的頭文件:#include <string.h>
2.1strcpy字符串拼接
原型:extern char *strcpy(char *dest,char *src);
功能:把src
所指由NULL結束的字符串復制到dest
所指的數組中。
說明:src和dest
所指內存區域不可以重疊且dest
必須有足夠的空間來容納src的字符串。
返回指向dest
的指針。
舉例:
void main(void){ char dest[50]; char *a = "china"; char *b = " is powerful!"; strcpy(dest, a); strcat(dest, b); printf("%s\n", dest); system("pause"); }
結果輸出:
china is powerful!
2.2strchr字符串中查找字符
原型:extern char *strchr(char *s,char c);
功能:查找字符串s中首次出現字符c的位置
說明:返回首次出現c的位置的指針,如果s中不存在c則返回NULL。
void main(void){ char *str = "I want go to USA!"; printf("%#x\n", str); //U元素的指針 //str+3 char* p = strchr(str, 'w'); if (p){ printf("索引位置:%d\n", p - str); } else{ printf("沒有找到"); } system("pause"); }
結果輸出:
0x877b30
索引位置:2
2.3strchr字符串中查找字符
原型:extern char *strstr(char *haystack, char *needle);
功能:從字符串haystack
中尋找needle
第一次出現的位置(不比較結束符NULL)。
說明:返回指向第一次出現needle
位置的指針,如果沒找到則返回NULL。
//strstr 從字符串haystack中尋找needle第一次出現的位置 void main(void){ char *haystack = "I want go to USA!"; char *needle = "to"; //U元素的指針 char* p = strstr(haystack, needle); if (p){ printf("索引位置:%d\n", p - haystack); } else{ printf("沒有找到"); } system("pause"); }
結果輸出:
索引位置:10
2.4更多用法...
//strset 把字符串s中的所有字符都設置成字符c void main(void){ char str[] = "internet change the world!"; _strset(str,'w'); printf("%s\n",str); system("pause"); } //strrev 把字符串s的所有字符的順序顛倒過來 void main(void){ char str[] = "internet change the world!"; _strrev(str); printf("%s\n", str); system("pause"); } //atoi 字符串轉為int類型 //atol():將字符串轉換為長整型值 void main(void){ char* str = "a78"; //int r = atoi(str); printf("%d\n", r); system("pause"); } // 字符串轉為double類型 void main(void){ char* str = "77b8b"; char** p = NULL; //char* p = str + 2; //參數說明:str為要轉換的字符串,endstr 為第一個不能轉換的字符的指針 double r = strtod(str,p); printf("%lf\n", r); printf("%#x\n", p); system("pause"); } //strupr轉換為大寫 void main(void){ char str[] = "CHINA motherland!"; _strupr(str); printf("%s\n",str); system("pause"); } //轉換為小寫 void mystrlwr(char str[],int len){ int i = 0; for (; i < len; i++){ //A-Z 字母 a-Z if (str[i] >= 'A' && str[i] <= 'Z'){ str[i] = str[i]-'A' + 'a'; } } } void main(void){ char str[] = "CHINA motherland!"; mystrlwr(str,strlen(str)); printf("%s\n", str); system("pause"); } //練習:刪除字符串中指定的字符 void delchar(char *str, char del){ char *p = str; while (*str != '\0') { if (*str != del) { *p++ = *str; } str++; } *p = '\0'; } //刪除最后一個字符 int main() { char str[] = "vencent ppqq"; delchar(str,'t'); printf("%s\n", str); system("pause"); } //Java String replaceAll //StringBuffer buff.deleteCharAt(buff.length()-1); //刪除最后一個字符 void main(void){ char str[] = "internet,"; str[strlen(str) - 1] = '\0'; printf("%s\n", str); //作業:realloc實現StringBuffer的拼接,而不是一開始開辟一個很大的數組 //結構體StringBuffer system("pause"); } //memcpy 由src所指內存區域復制count個字節到dest所指內存區域 void main(void){ char src[] = "C,C++,Java"; char dest[20] = {0}; //字節 memcpy(dest,src,5); printf("%s\n",dest); system("pause"); } //memchr 從buf所指內存區域的前count個字節查找字符ch。 void main(void){ char src[] = "C,C++,Java"; char ch = 'C'; //字節 (分段截取) char* p = memchr(src+3, ch, 5); if (p){ printf("索引:%d\n", p - src); } else{ printf("找不到\n"); } system("pause"); } //memmove 由src所指內存區域復制count個字節到dest所指內存區域。 void main(){ char s[] = "Michael Jackson!"; //截取的效果 memmove(s, s + 8, strlen(s) - 8 - 1); s[strlen(s) - 8] = 0; printf("%s\n", s); getchar(); } //在字符串s1中尋找字符串s2中任何一個字符相匹配的第一個字符的位置,空字符NULL不包括在內 void main(){ char *s1 = "Welcome To Beijing"; char *s2 = "to"; char *p; p = strpbrk(s1, s2); if (p) printf("%s\n", p); else printf("Not Found!\n"); p = strpbrk(s1, "Da"); if (p) printf("%s", p); else printf("Not Found!"); getchar(); }
原文鏈接:https://juejin.cn/post/7038465634683387912
相關推薦
- 2022-08-29 Python可視化神器pyecharts繪制水球圖_python
- 2023-11-21 MAC 打開不開第三方應用XXX can’t be opened because the ident
- 2022-07-03 Go?的入口函數和包初始化的使用_Golang
- 2022-09-15 Android?Jetpack庫剖析之LiveData組件篇_Android
- 2022-09-17 ASP.NET?Core實現AES-GCM加密算法_實用技巧
- 2022-11-26 C#?WinForm實現自動更新程序的方法詳解_C#教程
- 2022-08-14 PyTorch中torch.utils.data.DataLoader簡單介紹與使用方法_pytho
- 2022-09-07 基于域名的方式訪問Istio服務網格中的多個應用程序的方法詳解_相關技巧
- 最近更新
-
- 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同步修改后的遠程分支