Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1437919
  • 博文数量: 704
  • 博客积分: 10140
  • 博客等级: 上将
  • 技术积分: 6230
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-15 20:41
文章分类

全部博文(704)

文章存档

2013年(1)

2012年(16)

2011年(536)

2010年(151)

分类: LINUX

2010-07-30 15:33:42

zj@zj:~/C_parm/string/own_str/strchr$ cat strchr.c
/**
 * The strchr() function returns a pointer to the first occurrence of the
 * character c in the string s.
 * The strchr() and strrchr() functions return a pointer to the matched
 * character or NULL if the character is not found.
 * strchr.c
*/

 
#include<stdio.h>
#include<stdlib.h>

char* my_strchr(const char* s, int c);
char* my_strrchr(const char* s,int c);

int main()
{
   char* str = "1a,2a,3a,4a!";
   char c = 'a';
   printf("the first occurrence \'%c\' of %s is\n%s\n",c,str,my_strchr(str,c));
   printf("the last occurrence \'%c\' of %s is\n%s\n",c,str,my_strrchr(str,c));
   
  exit(EXIT_SUCCESS);
}

char* my_strchr(const char *s, int c)
{
   int i = 0;
   while((*(s+i)!=c)&&(*(s+i)!='\0'))
    i++;
   if(*(s+i)==c)
    return (s+i);
   else
    return NULL;
}

char* my_strrchr(const char* s,int c)
{
    char* last;
    last = my_strchr(s,c);
         while(my_strchr(last+1,c))
          {
     last=my_strchr(last+1,c);    
          }    
    return last;
}
zj@zj:~/C_parm/string/own_str/strchr$ ./strchr
the first occurrence 'a' of 1a,2a,3a,4a! is
a,2a,3a,4a!
the last occurrence 'a' of 1a,2a,3a,4a! is
a!
阅读(900) | 评论(0) | 转发(0) |
0

上一篇:c strcat

下一篇:c strstr

给主人留下些什么吧!~~