【C語言練習題】檢索字串
阿新 • • 發佈:2018-12-26
《C與指標》習題6.18.1
題目:函式在一個字串中進行搜尋,查詢所有在一個給定字元集合中出現的字元
函式原型:char *find_char( char const *source, char const *chars);
程式碼
#include <stdio.h> #include <assert.h> char *find_char( char const *source, char const *chars ); void main( void ) { char *p = NULL; p = find_char("hello_world","e"); if( NULL != p) printf("%c\n",*p); return ; } /*查詢字串中的指定字元 **source:目標字串 chars:源字元或者串 **返回 成功:匹配字元的地址 失敗:NULL */ char *find_char( char const *source, char const *chars ) { char const *paddr = source; assert( paddr ); //檢查地址有效性 while( *paddr != '\0' ) { if( *paddr == *chars ) { return (char *)paddr; } paddr++; } return NULL; }
演示