笔试题: 不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数
2015-11-11 22:09
176 查看
笔试题:
不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数。
函数接口声明如下:int my_strlen(const char *p);
strlen函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符'\0'。
而返回的长度大小不包括'\0'。
不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数。
函数接口声明如下:int my_strlen(const char *p);
strlen函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符'\0'。
而返回的长度大小不包括'\0'。
#include <stdio.h> #include <assert.h> //使用中间变量 //int my_strlen(const char *str) //{ // assert(str); // int count = 0; // while (*str++) // { // count++; // } // return count; // } //递归方法(不使用中间变量) int my_strlen(const char *str) { if (*str == '\0') { return 0; } else { return 1 + my_strlen(str + 1); } } int main() { char str[] = {0}; gets(str); int len = my_strlen(str); printf("%d\n", len); system("pause"); return 0; }
相关文章推荐
- Android shape 各种参数 详解
- 滤波方法 PF KF EKF UKF
- Eclipse常用快捷键
- 0019将字符串逆序操作
- 新浪微博SDK抛出异常-[__NSDictionaryM weibosdk_WBSDKJSONString]: unrecognized selector sent to instance 0x7fb
- php的exec命令中关于空格路径的问题
- 作业调度模拟程序
- RMI原理
- c语言之指针数组理解一
- 差点就坚持不下去了
- 多线程内容学习总结笔记
- seajs的使用1.0
- 对Cookie的理解
- android-Ultra-Pull-To-Refresh源码分析
- ios常用的工具 插件
- 将两个不同进程的窗口设置为父子关系
- Struts2中的Unable to load configuration错误的分析与解决方法
- SQL Server 2016里TempDb的提升
- Swift(十七、可选链)
- Android应用中平滑的手写效果实现