• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C字符串操作函数的实现详细解析

c语言 搞代码 4年前 (2022-01-06) 20次浏览 已收录 0个评论

以下是对C语言中字符串操作函数的实现进行了详细的分析介绍,需要的朋友可以过来参考下

1. strlen(),计算字符串长度  

 int strlen(const char string) { int i=0; while(string[i]) i++; return i; } 

2. strcpy(), 字符串拷贝.   

 char *strcpy(char *destination, const char *source) { while(*destinaton++=*source++); return (destination-1); }

3. strcat(), 字符串的连接.   

 char *strcat(char *target,const char *source) { char *original=target; while(*target) target++; // Find the end of the string while(*target++=*source++); return(original); } 

4. streql(), 判断两个字符串是否相等.   

 int streql(char *str1,char *str2) { while((*str1==*str2)&&(*str1)) { str1++; str2++; } return((*str1==NULL)&&(*str2==NULL)); } 

5. strchr(), 在字符串中查找某个字符.   

 char *strchr(const char *string,int letter) { while((*string!=letter)&(*string)) string++; return (string); }  

6. chrcnt(), 计算某个字符在字符串中出现的次数.  

 int chrcnt(const char *string,int letter) { int count=0; while(*string) if(*string==letter)count++; return count; } 

7. strcmp(), 判断两个字符串是否相等.  

 int strcmp(const char *str1,const char *str2) { while((*str1==*str2)&&(*str1)) { str1++; str2++; } if((*str1==*str2)&&(!*str1)) //Same strings return o; else if((*str1)&&(!*str2)) //Same but str1 longer return -1; else if((*str2)&&(!*str1)) //Same but str2 longer else return((*str1>*str2)?-1:1); } 

以上就是C字符串操作函数的实现详细解析的详细内容,更多请关注来源gaodai#ma#com搞*代#码网gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C字符串操作函数的实现详细解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址