strlen(s1)字符串有效字符个数,不包含结尾的’\0’
写成自己的函数
#include <stdio.h>
#include <string.h>
int mylen(const char*s){
//int cnt = 0;
int index = 0; //只用index作为计数也可以
while(s[index]!='\0'){
index++;
//cnt++;
}
//return cnt;
return index;
}
int main(int argc, char const *argv[]){
char s[] = "Hello, Peter Zhang";
printf("%d\n",mylen(s)); //结果为18
printf("%d\n",strlen(s));//结果为18
printf("%d\n",sizeof(s));//结果为19
return 0;
}
strcmp(s1,s2) 比较两个字符串的地址
写成自己的函数
- 用数组形式,while条件为1
#include <stdio.h>
#include <string.h>
int mycmp(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 main(int argc, char const *argv[]){
char s1[] = "abc";
char s2[] = "abc";
printf("%d\n", mycmp(s1,s2));
return 0;
}
- 用数组形式
#include <stdio.h>
#include <string.h>
int mycmp(const char*s1, const char*s2){
int idx=0;
while( s1[idx] == s2[idx] && s1[idx] != '\0'){
idx++;
}
return s1[idx] - s2[idx];
}
int main(int argc, char const *argv[]){
char s1[] = "abc";
char s2[] = "abc";
printf("%d\n", mycmp(s1,s2));
return 0;
}
- 用指针形式
// 用指针形式
#include <stdio.h>
#include <string.h>
int mycmp(const char*s1, const char*s2){
while( *s1 == *s2 && *s1 != '\0'){
s1++;
s2++;
}
return *s1 - *s2;
}
int main(int argc, char const *argv[]){
char s1[] = "abc";
char s2[] = "abc";
printf("%d\n", mycmp(s1,s2));
return 0;
}
stycpy 是字符串处理中极其广泛大量被使用的函数⭐️
-
char *strcpy(char *restrict dst, const char *restrict src);
-
把 src 的字符串拷贝到 dst
- restrict 表明 src 和 dst 不重叠(C99)
- 不能重叠的原因:当计算机是多核时,拷贝工作可能会分成好几段,让每个核去做其中的一段,此时要求 dst 和 src 不能重叠,如果重叠,分开拷贝的一段段之间就会冲突
不重叠
重叠
-
返回 dst
- 为了让strcpy的结果能参与到其他运算,为了能链起代码来
复制一个字符串
char dst = (char) malloc (strlen(src)+1);
strcpy(dst,src);
- malloc 先给dst动态分配一块内存,长度是strlen(src)+1, 一定要记得+1,用于放结尾的 ‘\0’
- malloc 返回类型为(char*)
- 再把 src 的内容拷贝到 dst
写成自己的函数
- 用数组形式
#include <stdio.h>
#include <string.h>
char* mycpy(char* dst, const char* src){
int idx = 0;
while(src[idx]) != '\0'){ // while(src[idx]!='\0') 即 while(src[idx])
dst[idx] = src[idx];
idx++;
}
dst[idx] = '\0';
return dst;
}
int main(int argc, char const *argv[]){
char s1[] = "abc";
char s2[] = "123";
mycpy(s1,s2);
printf("%s\n",s1 );//结果为123
return 0;
}
- 用指针形式
#include <stdio.h>
#include <string.h>
char* mycpy(char* dst, const char* src){
char* ret = dst;
while(*src != '\0'){ // 也可以写成while(*src)
//*dst = *src;
//dst++;
//src++;
//上面三行也可以合并为:
*dst++ = *src++;
}
*dst = '\0';
return ret;
}
int main(int argc, char const *argv[]){
char s1[] = "abc";
char s2[] = "123";
mycpy(s1,s2);
printf("%s\n",s1 );//结果为123
return 0;
}
strchr
char strchr(const char s, int c);
strrchr
char strrchr(const char s, int c);
返回NULL表示没找到
- 查找输出”hello“ 里的第1个 “l" -->输出llo
程序例:
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]){
char s[] = "hello";
char *p = strchr(s,'l');
printf("%s\n", p); // 输出为 llo
// %s字符串输出时,p的值是地址
//从该地址所指定的单元开始连续输出其中的内容(字符)
// 直至遇到'\0'为止
return 0;
}
或者程序例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[]){
char s[] = "hello";
char *p = strchr(s,'l');
char *t = (char*)malloc(strlen(p)+1); //申请strlen(p)+1 字节内存给t
strcpy(t,p); // 把p的内容拷贝给t
printf("%s\n", t); //输出t 结果为:llo
free(t);
return 0;
}
- 查找输出”hello“ 里的第2个 “l" -->输出lo
程序例:
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]){
char s[] = "hello";
char *p = strchr(s,'l');
p = strchr(p+1,'l'); //从p+1开始的字符串开始找 ‘l’
printf("%s\n", p); // 输出为 lo
return 0;
}
- 查找输出”hello“ 里的 ”he“
程序例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char const *argv[]){
char s[] = "hello";
char *p = strchr(s,'l');
char c = *p; // c储存 *p
*p = '\0'; // 让*p 变成 ’\0',则 s变成 “he\0lo"
char *t = (char *)malloc(strlen(s)+1);
strcpy(t,s); //把s拷贝给t
printf("%s\n", t);
free(t); // malloc 后记得free释放内存
return 0;
}
strstr 字符串中找字符串
char* strstr(const char *s1, const char *s2);
strcasestr
char* strcasestr(const char *s1, const char *s2);