急求一个C语言函数:判断一个字符串是否包含在另一个字符串当中

changhairen 2008-12-06 10:50:43
RT,请说明函数参数和返回值意义。
...全文
637 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jch8528563jch 2010-08-06
  • 打赏
  • 举报
回复
syslib.h==stdlib.h
zapdos 2008-12-07
  • 打赏
  • 举报
回复
KMP
bbspeterlee 2008-12-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 changhairen 的帖子:]
RT,请说明函数参数和返回值意义。
[/Quote]

可以直接使用库函数strstr(),若不允许,请看:
http://www.peterlee.com.cn/blog.php?getArticleID=83&getYearMonth=200811
Implementation of ANSI C String Library Functions (ANSI C 字符串库函数的实现)

/* Referenced from Microsoft CRT source code, 
efficient and easy to understand */
char* strstr(const char* cs, const char* ct)
{
char* cp = (char*)cs;
char* s1, * s2;

if ( !*ct )
return (char*)cs;

while ( *cp )
{
s1 = cp;
s2 = (char*)ct;

while ( *s1 && *s2 && *s1==*s2) )
++s1, ++s2;

if ( !*s2 )
return cp;

++cp;
}

return NULL;
}
zzzlll1983 2008-12-06
  • 打赏
  • 举报
回复
有答案了已经
wuyu637 2008-12-06
  • 打赏
  • 举报
回复
// strstr.c

#include <syslib.h>
#include <string.h>

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;
}
changhairen 2008-12-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kkndciapp 的回复:]
strstr
[/Quote]

strstr("dwddrv\ACC\src\acc.c","acc.c") 不好使呀
kkndciapp 2008-12-06
  • 打赏
  • 举报
回复
strstr
WingForce 2008-12-06
  • 打赏
  • 举报
回复
Find a substring.

char *strstr( const char *string, const char *strCharSet );
wchar_t *wcsstr( const wchar_t *string, const wchar_t *strCharSet );
Parameters
string
Null-terminated string to search.
strCharSet
Null-terminated string to search for.
Libraries
All versions of the C run-time libraries.

Return Values
Each of these functions returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.

Remarks
The strstr function returns a pointer to the first occurrence of strCharSet in string. The search does not include terminating null characters. wcsstr is the wide-character version of strstr. The arguments and return value of wcsstr are wide-character strings. These two functions behave identically otherwise.


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
piginthetree 2008-12-06
  • 打赏
  • 举报
回复
c库好像有这样的函数啊
ayw215 2008-12-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 changhairen 的回复:]
syslib.h 这个文件包不进去


能否给个函数 能比较像这种形式的字符串 "dwddrv\ACC\src\acc.c" "acc.c"


引用 5 楼 wuyu637 的回复:
// strstr.c

#include <syslib.h>
#include <string.h>

main()
{
char *s="Golden Global View";
char *l="lob";
char *p;

clrscr();

p=strstr(s,l…
[/Quote]
那就不要嘛
changhairen 2008-12-06
  • 打赏
  • 举报
回复
syslib.h 这个文件包不进去


能否给个函数 能比较像这种形式的字符串 "dwddrv\ACC\src\acc.c" "acc.c"


[Quote=引用 5 楼 wuyu637 的回复:]
// strstr.c

#include <syslib.h>
#include <string.h>

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;
}
[/Quote]

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧