c语言中strlen的几种写法
2014-08-22 14:56
253 查看
一.指针 1(指针的减法运算)2个指针的相减得到字符串的长度 #include <stdio.h> #include <stdlib.h> int strlen(char *s); /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { return 0; } int strlen(char *s){ char *p=s; while(*p!='\0'){ p++; } return p-s; } 2 1个指针和1个参数 通过参数的累加得到字符串的长度<pre name="code" class="objc">#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int strlen(char *s); int main(int argc, char *argv[]) { return 0; } int strlen(char *s){ int n; for(n=0;*s!='\0';s++){ n++; } return n; }
二.数组和一个参数
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int strlen(char s[]); int main(int argc, char *argv[]) { return 0; } int strlen(char s[]){ int i; i=0; while(s[i]!='\0'){ i++; } return i; }
相关文章推荐
- 黑马程序员-C语言之求三个数中的最大值的几种C语言写法
- C语言中strlen的几种写法
- C语言中将三个数字进行排序的几种写法
- 基础快速过 之C语言 一:什么是 main 和main的几种写法。
- 【C语言】++(a++)的写法是错的
- CSS中几种中文字体的英文写法.
- 交换函数几种写法
- java中单例的几种写法
- 常用几种数据库,取前10条记录的sql语句写法
- HTML中调用JavaScript的几种情况和规范写法
- 存储过程几种写法
- 【C/C++】C语言中qsort的cmp函数写法汇总
- 四选一电路的几种写法
- WSGI应用常见的几种写法
- HTML中调用JavaScript的几种情况和规范写法
- C语言定义数组的几种形式
- Java保留两位小数的几种写法总结
- if-else(python)的几种写法
- 详解Nginx 动态 DNS 反向代理的几种写法
- C语言实现几种编码形式的互转 -----(整理加原创)