在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.0 字符串复制1.1 strcpy() 完全复制原型:char* strcpy(char* dest, char* src); 1 int main() 2 { 3 char src[]="abcde"; 4 char dest[100]; 5 strcpy(dest,src); 6 printf("%s",dest); 7 //输出>> abcde 8 } 1.2 strncpy() 部分复制原型: char* strncpy(char* dest, char* src, int size_tn);
如果n=(0, src串长度),src的前n个字符复制到dest中 但是由于没有NULL字符,所以直接访问dest串会发生栈溢出的异常情况 这时,一般建议采取memset将dest的全部元素用null填充
1 int main() 2 { 3 char src[]="SWT is so great! We should % him everyday."; 4 char dest[100]; 5 6 memset(dest,0,sizeof(dest)); 7 strncpy(dest,src,10); 8 printf("%s\n",dest); 9 //输出>>SWT is so 10 11 memset(dest,0,sizeof(dest)); 12 strncpy(dest,src,sizeof(src)); 13 printf("%s\n",dest); 14 //输出>>SWT is so great! We should % him everyday. 15 16 memset(dest,0,sizeof(dest)); 17 strncpy(dest,src,sizeof(dest)); 18 printf("%s\n",dest); 19 //输出>>SWT is so great! We should % him everyday. 20 21 char des[10]; 22 memset(des,0,sizeof(des)); 23 strncpy(des,src,sizeof(src)); 24 printf("%s\n",des); 25 //exe停止工作 26 } 2.0 字符串合并2.1 strcat() 完全合并原型: char* strcat(char* dest, char* src); 1 int main() 2 { 3 char src[]="OI!"; 4 char dest[]="We like "; 5 strcat(dest,src); 6 printf("%s",dest); 7 //输出>>We like OI! 8 } 2.2 strncat() 部分合并原型: char* strncat(char* dest, char* src, int n); 1 int main() 2 { 3 char src[]="Please login in here!#$%@$@%@#$@%"; 4 char dest[]="Welcome to the largest talking room of SLYZ! "; 5 strncat(dest,src,21); 6 printf("%s",dest); 7 //输出>>Welcome to the largest talking room of SLYZ! Please login in here! 8 } 3.0 字符串查找3.1 strchr() 查找字符原型: char* strchr(char* s, char c); 1 int main() 2 { 3 char src[]="Can you find some thing?"; 4 int t=strchr(src,'?')-src; 5 printf("%d",t); 6 //输出>>23 7 } 3.2 strchr() 查找字符串原型: char* strstr(char* haystack, char* needle); 1 int main() 2 { 3 char src[]="Can you find some thing?"; 4 int t=strstr(src,"thing")-src; 5 printf("%d",t); 6 //输出>>18 7 } 4.0 字符串比较4.1 strcmp() 区分大小写原型: int strcmp(char* s1, char* s2); 1 int main() 2 { 3 char src[]="Hello!"; 4 char dest[]="hello!"; 5 if (!strcmp(src,dest)) printf("SAME"); 6 else printf("DIFFERENT"); 7 //输出>>DIFFERENT 8 } 4.2 stricmp() 不区分字母的大小写原型: int stricmp(char* s1, char* s2); 1 int main() 2 { 3 char src[]="Hello!"; 4 char dest[]="hello!"; 5 if (!stricmp(src,dest)) printf("SAME"); 6 else printf("DIFFERENT"); 7 //输出>>SAME 8 } 4.3 strncmp() 部分比较,区分字母的大小写原型: int strncmp(char* s1, char* s2, int n); 1 int main() 2 { 3 char src[]="A APPLE A DAY."; 4 char dest[]="a apple a day."; 5 if (strncmp(src,dest,8)==0) printf("SAME"); 6 else printf("DIFFERENT"); 7 //输出>>DIFFERENT 8 } 4.4 strnicmp() 部分比较,不区分字母的大小写原型: int strnicmp(char* s1, char* s2, int n); 1 int main() 2 { 3 char src[]="A APPLE A DAY."; 4 char dest[]="a apple a day."; 5 if (strnicmp(src,dest,8)==0) printf("SAME"); 6 else printf("DIFFERENT"); 7 //输出>>SAME 8 } 5.0 字符串转化5.1 strupr() 小写转大写原型: char* strupr(char* s); 1 int main() 2 { 3 char s[]="Let's % SWT together!"; 4 strupr(s); 5 printf("%s",s); 6 //输出>>LET'S % SWT TOGETHER! 7 } 5.2 strlwr() 大写转小写原型: char* strlwr(char* s); 1 int main() 2 { 3 char s[]="Let's % SWT together!"; 4 strlwr(s); 5 printf("%s",s); 6 //输出>>let's % swt together! 7 } 5.3 strrev() 字符串倒置原型: char* strrev(char* s); 1 int main() 2 { 3 char s[]="!uoy kcor lliw eW"; 4 strrev(s); 5 printf("%s",s); 6 //输出>>We will rock you! 7 } 5.4 strset() 字符设置原型: char* strset(char* s, char c); 1 int main() 2 { 3 char s[]="(!@&*#$^*@#&^(!@#*))"; 4 strset(s,'%'); 5 printf("%s",s); 6 //输出>>%%%%%%%%%%%%%%%%%%%% 7 }
参考链接:https://blog.csdn.net/wu_tongtong/article/details/79004668 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论