标签:style color io os ar strong sp div c
数组元素的结束符为‘\0‘,串的结束符为NULL
一、strlen
#include <iostream>
using namespace std;
long h_strlen(constchar*str){
assert(str!=NULL);
const char *s = str;
while (*s++);
return (s - str - 1);
}
int main(int argc, const char * argv[])
{
char s1[] = "hello";
char s2[] = "123";
int s1_len = h_strlen(s1);
int s2_len = h_strlen(s2);
printf("s1_len:%d\n",s1_len);
printf("s2_len:%d\n",s2_len);
}
二、strstr
// 在字符串s1中寻找字符串2,若找到返回位置,否则返回Null
char *h_strstr(char*str,char *subStr){
while (*str !=‘\0‘) {
char *p = str;
char *q = subStr;
char *res =NULL;
if (*p == *q) {
res = p;
while (*q !=‘\0‘ && *p == *q){
p++;
q++;
}
if (*q ==‘\0‘)
returnres;
}
str++;
}
}
二、strcmp
long h_strcmp(constchar *src,constchar *dst){
assert(src != NULL&& dst != NULL);
while (*src && *dst && (*src ==*dst)) {
src++;
dst++;
}
return (*dst - *src);
}
标签:style color io os ar strong sp div c
原文地址:http://blog.csdn.net/paulery2012/article/details/39897397