关闭

哈哈,原来模式匹配就是一个 strstr(string, str);

标签: stringmicrosoftnulloutput
1579人阅读 评论(0) 收藏 举报

Example

/* STRSTR.C */

#include <string.h>
#include <stdio.h>

char str[] =    "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

void main( void )
{
   char *pdest;
   int  result;
   printf( "String to be searched:/n/t%s/n", string );
   printf( "/t%s/n/t%s/n/n", fmt1, fmt2 );
   pdest = strstr( string, str );
   result = pdest - string + 1;
   if( pdest != NULL )
      printf( "%s found at position %d/n/n", str, result );
   else
      printf( "%s not found/n", str );
}

Output

String to be searched:
   The quick brown dog jumps over the lazy fox
            1         2         3         4         5
   12345678901234567890123456789012345678901234567890

lazy found at position 36
//在我们看到处理字符串的函数时, 就想起了microsoft的实现·
char * __cdecl strstr (
        const char * str1,
        const char * str2
        )
{
        char *cp = (char *) str1;
        char *s1, *s2;
        if ( !*str2 )
            return((char *)str1);
        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;
                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;
                if (!*s2)
                        return(cp);
                cp++;
        }
        return(NULL);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:243556次
    • 积分:3018
    • 等级:
    • 排名:第11589名
    • 原创:39篇
    • 转载:63篇
    • 译文:0篇
    • 评论:42条
    文章分类
    最新评论