1、字符串复制
void copy_str1(char *from, char *to)
{
for (; *from!='\0'; from++, to++)
{
*to = *from;
}
*to = '\0';
return ;
}
void copy_str2(char *from, char *to)
{
for (; *from!='\0';)
{
*to++ = *from++; // 先 *to = *from; 再from++, to++
}
*to = '\0'; //
return ;
}
void copy_str3(char *from, char *to)
{
while( (*to = *from) != '\0' )
{
from ++;
to ++;
}
}
void copy_str4(char *from , char *to)
{
while ( (*to++ = *from++) != '\0')
{
;
}
}
void copy_str5(char *from , char *to)
{
//*(0) = 'a';
while ( (*to++ = *from++) )
{
;
}
}
void copy_str5_err(char *from , char *to)
{
//*(0) = 'a';
while ( (*to++ = *from++) )
{
;
}
printf("from:%s \n", from);
}
//不要轻易改变形参的值, 要引入一个辅助的指针变量. 把形参给接过来.....
int copy_str6(char *from , char *to)
{
//*(0) = 'a';
char *tmpfrom = from;
char *tmpto = to;
if ( from == NULL || to == NULL)
{
return -‐1;
}
while (*tmpto++ = *tmpfrom++) ; //空语句
printf("from:%s \n", from);
return 0;
}
2、字符串查找
int len(char *p)
{
int ncount = 0;
//初始化 让p指针达到查找的条件
char *p = "2abcd3333322qqqabcd";
while ( (p = strstr(p, "abcd")) != NULL )
{
ncount ++;
p = p + strlen("abcd"); //让p指针达到查找的条件
if (*p == '\0')
{
break;
}
}
return ncount;
}
3、字符串反转
char* inverse(char *str)
{
int length = strlen(str);
char *p1 = NULL;
char *p2 = NULL;
char tmp_ch;
if (str == NULL) {
return NULL;
}
p1 = str;
p2 = str + length -‐1;
while (p1 < p2) {
tmp_ch = *p1;
*p1 = *p2;
*p2 = tmp_ch;
++p1;
-‐-‐p2;
}
return str;
}