Chinaunix首页 | 论坛 | 博客
  • 博客访问: 918288
  • 博文数量: 132
  • 博客积分: 9976
  • 博客等级: 中将
  • 技术积分: 1781
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-30 20:40
文章分类

全部博文(132)

文章存档

2013年(1)

2011年(1)

2010年(15)

2009年(77)

2008年(36)

2007年(2)

我的朋友

分类: C/C++

2009-04-07 15:43:26

C语言字符串操作函数

=============================================================================
1. strchr
=============================================================================
cjash@linux-h3i2:c\> cat strchr_test.c
#include
#include
#include

int main(void)
{
  char *str_var = "hello-world!!!_this-is-strchr-test.";
  char *strchr_ret = NULL;
  strchr_ret = strchr(str_var, '-');
  printf("now do strchr test1...\n\n");
  printf("str_var = [%s]\n", str_var);
  printf("strchr_ret = [%s]\n\n", strchr_ret);

  printf("now do strchr test2...\n\n");
  strchr_ret = strrchr(str_var, '_');
  printf("str_var = [%s]\n", str_var);
  printf("strchr_ret = [%s]\n\n", strchr_ret);


  strchr_ret = strrchr(str_var, '-');
  printf("now do strrchr test1...\n\n");
  printf("str_var = [%s]\n", str_var);
  printf("strchr_ret = [%s]\n\n", strchr_ret);

  printf("now do strchr test2...\n\n");
  strchr_ret = strrchr(str_var, '_');
  printf("str_var = [%s]\n", str_var);
  printf("strchr_ret = [%s]\n\n", strchr_ret);

  return 0;
}
cjash@linux-h3i2:c\> ./strchr_test
now do strchr test1...

str_var = [hello-world!!!_this-is-strchr-test.]
strchr_ret = [-world!!!_this-is-strchr-test.]

now do strchr test2...

str_var = [hello-world!!!_this-is-strchr-test.]
strchr_ret = [_this-is-strchr-test.]

now do strrchr test1...

str_var = [hello-world!!!_this-is-strchr-test.]
strchr_ret = [-test.]

now do strchr test2...

str_var = [hello-world!!!_this-is-strchr-test.]
strchr_ret = [_this-is-strchr-test.]

cjash@linux-h3i2:c\>
-----------------------------------------------------------------------------
相关资料:
-----------------------------------------------------------------------------
from:

  原型:extern char *strchr(char *s,char c);  
  用法:#include
  功能:查找字符串s中首次出现字符c的位置
  说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
  举例:

      // strchr.c
     
      #include
      #include

      main()
      {
        char *s="Golden Global View";
        char *p;
       
        clrscr();
       
        strchr(s,'V');
        if(p)
          printf("%s",p);
        else
          printf("Not Found!");

        getchar();
        return 0;
      }
     
  相关函数:memchr
-----------------------------------------------------------------------------

NAME
       strchr, strrchr, strchrnul - locate character in string

SYNOPSIS
       #include

       char *strchr(const char *s, int c);

       char *strrchr(const char *s, int c);

       #define _GNU_SOURCE
       #include

       char *strchrnul(const char *s, int c);

DESCRIPTION
       The strchr() function returns a pointer to the first occurrence of the character c in the string s.

       The strrchr() function returns a pointer to the last occurrence of the character c in the string s.

       The  strchrnul() function is like strchr() except that if c is not found in s, then it returns a pointer to the null
       byte at the end of s, rather than NULL.

       Here "character" means "byte"; these functions do not work with wide or multi-byte characters.

RETURN VALUE
       The strchr() and strrchr() functions return a pointer to the matched character or  NULL  if  the  character  is  not
       found.

       The  strchrnul()  function returns a pointer to the matched character, or a pointer to the null byte at the end of s
       (i.e., s+strlen(s)) if the character is not found.

CONFORMING TO
       strchr() and strrchr() are in SVr4, 4.3BSD, C89, C99.  strchrnul() is a GNU extension.

SEE ALSO
       index(3), memchr(3), rindex(3),  strlen(3),  strpbrk(3),  strsep(3),  strspn(3),  strstr(3),  strtok(3),  wcschr(3),
       wcsrchr(3), feature_test_macros(7)

COLOPHON
       This  page  is  part  of release 2.79 of the Linux man-pages project.  A description of the project, and information
       about reporting bugs, can be found at



GNU
-----------------------------------------------------------------------------

=============================================================================
2. strstr
=============================================================================
cjash@linux-h3i2:c\> cat strstr_test.c
#include
#include
#include

int main(void)
{
  char *str_var = "hello-world!!!_this-is-strstr-test.";
  char *strstr_ret = NULL;
  strstr_ret = strstr(str_var, "is");
  printf("str_var = [%s]\n", str_var);
  printf(" strstr(str_var, \"is\") \n");
  printf("strstr_ret = [%s]\n\n", strstr_ret);

  strstr_ret = strstr(str_var, "this");
  printf(" strstr(str_var, \"this\") \n");
  printf("strstr_ret = [%s]\n\n", strstr_ret);


  strstr_ret = strstr(str_var, "world");
  printf(" strstr(str_var, \"world\") \n");
  printf("strstr_ret = [%s]\n\n", strstr_ret);

  strstr_ret = strstr(str_var, "strstr");
  printf(" strstr(str_var, \"strstr\") \n");
  printf("strstr_ret = [%s]\n\n", strstr_ret);

  return 0;
}
cjash@linux-h3i2:c\> ./strstr_test
str_var = [hello-world!!!_this-is-strstr-test.]
now do strstr test1...

 strstr(str_var, "is")
strstr_ret = [is-is-strstr-test.]

now do strstr test2...

 strstr(str_var, "this")
strstr_ret = [this-is-strstr-test.]

now do strrchr test1...

 strstr(str_var, "world")
strstr_ret = [world!!!_this-is-strstr-test.]

now do strstr test2...

 strstr(str_var, "strstr")
strstr_ret = [strstr-test.]

cjash@linux-h3i2:c\>
-----------------------------------------------------------------------------
material:

-----------------------------------------------------------------------------
STRSTR(3)                                        Linux Programmer's Manual                                        STRSTR(3)



NAME
       strstr, strcasestr - locate a substring

SYNOPSIS
       #include

       char *strstr(const char *haystack, const char *needle);

       #define _GNU_SOURCE

       #include

       char *strcasestr(const char *haystack, const char *needle);

DESCRIPTION
       The  strstr()  function  finds the first occurrence of the substring needle in the string haystack.  The terminating
       '\0' characters are not compared.

       The strcasestr() function is like strstr(), but ignores the case of both arguments.

RETURN VALUE
       These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.

CONFORMING TO
       The strstr() function conforms to C89 and C99.  The strcasestr() function is a non-standard extension.

BUGS
       Early versions of Linux libc (like 4.5.26) would not allow an empty needle argument for  strstr().   Later  versions
       (like 4.6.27) work correctly, and return haystack when needle is empty.

SEE ALSO
       index(3),  memchr(3),  rindex(3),  strchr(3), strcasecmp(3), strpbrk(3), strsep(3), strspn(3), strtok(3), wcsstr(3),
       feature_test_macros(7)

COLOPHON
       This page is part of release 2.79 of the Linux man-pages project.  A description of  the  project,  and  information
       about reporting bugs, can be found at



GNU                                                      2005-04-05                                               STRSTR(3)

-----------------------------------------------------------------------------
from:

strstr
   
  原型:extern char *strstr(char *haystack, char *needle);   
  用法:#include
  功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。
  说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
  举例:
      // strstr.c
     
      #include
      #include

      main()
      {
        char *s="Golden Global View";
        char *l="lob";
        char *p;
       
        clrscr();
       
        p=strstr(s,l);
        if(p)
          printf("%s",p);
        else
          printf("Not Found!");

        getchar();
        return 0;
      }
     
  相关函数:strchr,strpbrk,strtok
-----------------------------------------------------------------------------
from: http://blog.minidx.com/2008/02/03/470.html

C语言中利用strstr函数进行字符串分割

By Minidxer | February 3, 2008

在前面C语言中利用strtok函数进行字符串分割介绍的strtok函数,
比较适合多个字符(也就是字符串)作分隔符的场合,而很多时候
我们仅仅需要某一个特定的字符来分割字符串,当然利用strtok也
可以实现,不过这里介绍的strstr效率上来说更加适合。

原型:extern char *strstr(char *haystack, char *needle);
所在头文件:#include
功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。

具体使用例子:

   1. #include
   2. #include
   3. 
   4. int main(int argc,char **argv)
   5. {
   6. char *haystack="aaa||a||bbb||c||ee||";
   7. char *needle="||";
   8. char* buf = strstr( haystack, needle);
   9. while( buf != NULL )
  10. {
  11.     buf[0]='\0';
  12.     printf( "%s\n ", haystack);
  13.     haystack = buf + strlen(needle);
  14.     /* Get next token: */
  15.     buf = strstr( haystack, needle);
  16. }
  17.    return 0;
  18. }
-----------------------------------------------------------------------------
from: ~anhe/DOCU/C/C/////EXAMPLES/strstr.c

/*****************************************************************
*
* Purpose: Program to demonstrate the 'strstr' function.
* Author: M J Leslie
* Date: 18-Jun-94
*
*****************************************************************/

#include
#include

main()
{
char string[]="string to search";
char test[]="sear";

/* strstr returns a pointer into 'string'
* if 'test' is found' if not found, NULL
* is returned. */

if (strstr(string, test)) puts("String found");

}

-----------------------------------------------------------------------------
from: strstr.c
- bash-3.0/lib/sh


/* Copyright (C) 1994, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/*
 * My personal strstr() implementation that beats most other algorithms.
 * Until someone tells me otherwise, I assume that this is the
 * fastest implementation of strstr() in C.
 * I deliberately chose not to comment it.  You should have at least
 * as much fun trying to understand it, as I had to write it :-).
 *
 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */

#if HAVE_CONFIG_H
# include
#endif

#if defined _LIBC || defined HAVE_STRING_H
# include
#endif
#include

typedef unsigned chartype;

#undef strstr

char *
strstr (const char *phaystack, const char *pneedle)
{
  register const unsigned char *haystack, *needle;
  register chartype b, c;

  haystack = (const unsigned char *) phaystack;
  needle = (const unsigned char *) pneedle;

  b = *needle;
  if (b != '\0')
    {
      haystack--;                               /* possible ANSI violation */
      do
        {
          c = *++haystack;
          if (c == '\0')
            goto ret0;
        }
      while (c != b);

      c = *++needle;
      if (c == '\0')
        goto foundneedle;
      ++needle;
      goto jin;

      for (;;)
        {
          register chartype a;
          register const unsigned char *rhaystack, *rneedle;

          do
            {
              a = *++haystack;
              if (a == '\0')
                goto ret0;
              if (a == b)
                break;
              a = *++haystack;
              if (a == '\0')
                goto ret0;
shloop:;    }
          while (a != b);

jin:      a = *++haystack;
          if (a == '\0')
            goto ret0;

          if (a != c)
            goto shloop;

          rhaystack = haystack-- + 1;
          rneedle = needle;
          a = *rneedle;

          if (*rhaystack == a)
            do
              {
                if (a == '\0')
                  goto foundneedle;
                ++rhaystack;
                a = *++needle;
                if (*rhaystack != a)
                  break;
                if (a == '\0')
                  goto foundneedle;
                ++rhaystack;
                a = *++needle;
              }
            while (*rhaystack == a);

          needle = rneedle;                /* took the register-poor approach */

          if (a == '\0')
            break;
        }
    }
foundneedle:
  return (char*) haystack;
ret0:
  return 0;
}

-----------------------------------------------------------------------------

=============================================================================

=============================================================================

-----------------------------------------------------------------------------

=============================================================================




=============================================================================

=============================================================================

-----------------------------------------------------------------------------

=============================================================================
阅读(1756) | 评论(0) | 转发(0) |
0

上一篇:关于C的思考(转)

下一篇:ntop and n2n

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