标签:assert def ++ sys color [] sse else main
1、模拟实现Strlen函数
a#include<stdio.h>
#include<assert.h> int my_strlen(char* arr) { assert(arr != NULL); char* x = arr; while (*arr++ != ‘\0‘) { ; } return arr - x - 1; } int main() { char arr[] = "abcdef"; int ret = my_strlen(arr); printf("%d", ret);return 0; }
b、运用函数递归来实现
1 #include<stdio.h> 2 #include<assert.h> 3 4 int my_strlen(char* arr) 5 { 6 assert(arr != NULL); 7 if(*arr == ‘\0‘) 8 return 0; 9 else 10 return 1 + my_strlen(arr + 1); 11 } 12 13 14 int main() 15 { 16 char arr[] = "abcdef"; 17 int ret = my_strlen(arr); 18 printf("%d",ret); 19 return 0; 20 }
c、用计数器方法实现
#include<stdio.h> #include<assert.h> int my_strlen(char* arr) { assert(arr != NULL); int count = 0; while (*arr != ‘\0‘) { count++; arr++; } return count; } int main() { char arr[] = "abcdef"; int ret = my_strlen(arr); printf("%d", ret); system("pause"); return 0; }
标签:assert def ++ sys color [] sse else main
原文地址:https://www.cnblogs.com/yishengPan/p/9162608.html