strdup和strchr和strncmp和strstr

strdup复制字符串比起strcpy,strdup可以复制给没有分配内存的指针,最后这个分给这个指针的空间释放掉(但很怪我在VC6上实验即使分配了内存,strdup依然可以进行复制,也许跟平台有关);

strchr找到指定的字符,返回指向该字符的指针;
strncmp比较前N个字符,相等输出0
 
以下是vc6上的实验:
#include <stdio.h>
#include <string.h>
void main()
{
char temp[32];
char *s = temp;
char *p,c='v';
memset(temp,0,sizeof(temp));
strcpy(temp,"Goldven Global View");//注意这里用了sgtrcpy,故temp是事先要配内存的。
p=strchr(s,c);
if(p)
printf("%s\n",p);
else
printf("Not Found!");
}
 
以下代码来自x264
char *name="you are the god_girl";
char *name_buf;
  if( strchr( name, '_' ) ) // s/_/-/g
    {
int aa=10;
        char *p;
        name_buf = strdup(name);
        while( (p = strchr( name_buf, '_' )) )
            *p = '-';
        name = name_buf;
    }
 
 
printf("%s\n",name);
printf("%s\n",name_buf);
 
输出都是you are the god-girl
x264这段代码只是将输入name里含有的下划线变成中线而已。
 
另外
char *name="no-my girl";
int i;
 if( (!strncmp( name, "no-", 3 ) && (i = 3)) ||
        (!strncmp( name, "no", 2 ) && (i = 2)) )
    {
        name += i;
    }
 
 printf("%s\n",name);
 printf("%d",i);
这小段也有点意思,if后的这段语句先检查有没no,有就i=2,完了再看有没no-,有则令i=3,即从右向左结合的方式。故若有no-则必输出i=3,而不是 =2;
 
对strstr,strpbrk的实验:
#include<stdio.h>
#include<string.h>
void main()
{
char *name="YOU NING";
char *p;
char *p1;
p=strstr(name,"OU");
p1=strpbrk(name,"OU");
printf("%s\n",p);
printf("%s\n",p1);
}
输出都是
OU NING

转载于:https://www.cnblogs.com/yurius/archive/2013/02/24/2924166.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值