慕课c语言笔记之字符串函数-----伏虎山真人

*所有内容都在代码块里,可以复制下来,去掉/测试
文中内容是我近期学习c语言所留下来的笔记,上传平台是为了保存防止误删造成笔记丢失,自学不易,跪求浏览笔记的朋友给个赞

//知识点一:string是一个专门处理字符串的c语言库
//#include<string.h>
//常用函数有strlen,strcmp,strcpy,strcat,strchr,strstr
//知识点二:strlen测量字符串长度
//size_t strlen(const char*s)const为常量不能改变
//返回s的字符串长度(不包括结尾的0)
/*#include<string.h>
#include<stdio.h>
int mylen(const char*s)//strlen的内部实现类似于myslen,起一个计数的原理
{
    int idx=0;
while(s[idx]!='\0')
{
    idx++;
}
return idx;

}
int main()
{
char line[]="hello";
printf("strlen=%lu\n",strlen(line));
printf("sizeof=%lu\n",sizeof(line));
printf("mylen=%lu\n",mylen(line));
return 0;
}*/
//知识点二 strcmp函数
//int strcmp(const char*s2,const char*s2)
//比较两个字符串,返回值:若s1==s2,返回0,若s1>s2,返回1,若s1<s2,返回-1
/*#include<string.h>
#include<stdio.h>
int mycmp(const char*s1,const char*s2)//近视于strcmp的内部实现机制
{
while(*s1==*s2&&*s1!='\0')//用指针直接来实现比较
{
    s1++;
    s2++;
}
return *s1-*s2;

}
int main()
{
    int i;
    char s1[]="abc";
    char s2[]="abc";
    char s3[]="bbc";
    char s4[]="ABC";
    //printf("%d\n",s1==s2);当两个字符串用==来比较关系的时候是看两个的关系是否是相等的
    printf("%d\n",strcmp(s1,s2));//s1和s2相等
    printf("%d\n",strcmp(s1,s3));//s1小于s3
    printf("%d\n",strcmp(s1,s4));//s1大于s4
    printf("%d\n",s1-s2);
    //注意字符串比较的是他们ASCII码,比较顺序是从头开始一个字符一个字符的比较
    return 0;

}*/
//知识点三 strcpy函数
//char*strcpy(char*restrict dst,const char*restrict src);
//把src的字符串拷贝到dst
//restrict表示src和dst不重叠即(restrict是c99的新关键字)
//返回改变后的dst
//为了能连起代码来
#include<stdio.h>
#include<string.h>
int main()
{
char *src;
char*dst=(char*)malloc(strlen(src)+1);//malloc动态分配内存

}
//知识点五函数strcat
//char*strcat(char*restrict s1,const char*restrict)
//把s2拷贝到s1后面,连接成一个长字符串
//返回s1
//s1必须有足够的空间
//strcmp,strcat和strcpy不安全进场发生越界情况,所以使用strncmp,strncpy和strncat
//char*strncpy(char*restrict dst,const char*restrict src,size_t n)
//char*strncat(char*restrict dst,const char*restrict src,size_t n)
//char*strncmp(char*restrict dst,const char*restrict src,size_t n)
//这三个对操作的数量进行了设置
//知识点六 字符串中找字符
//char*strchr(const char*s,int c)
//char*strrchr(const char*s,int c)
//返回NULL表示没有找到

发布了10 篇原创文章 · 获赞 8 · 访问量 234

猜你喜欢

转载自blog.csdn.net/weixin_46325250/article/details/105409044