请各位大侠帮忙编写个程序,在字符串str1中找指定的字符串str2,并返回str2在str1中首次找到的位置。
如题,请各位大侠帮帮忙!
我弄了一个,不过感觉还有更改的地方
https://bbs.bccn.net/thread-280363-1-1.html
你看看吧
/* STRSTR.C from MSDN */ #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