看看有啥更好的方法没?
#include <stdio.h>#include <string.h>
char *find_char(char const *source, char const *chars);
int main()
{
char **ps;
char *result;
char *test[] = {"www.,
"www.bccn.net",
"www.,
"www.,
"www.google.tw",
"mil.news.,
NULL};
char chars[] = "pig";
ps = test;
while (*ps != NULL) {
result = find_char(*ps, chars);
if (result != NULL)
printf("%s, %s, %c\n", *ps, chars, *result);
ps++;
}
return 0;
}
char *find_char (char const *source, char const *chars)
{
char *ps;
char *pc;
/* 如果任一字符串为NULL或者为空,就返回NULL */
if ((chars == NULL) || (source == NULL) ||
(strlen(chars) == 0) || (strlen(source) == 0))
return NULL;
/* 返回第一个匹配的字符 */
for (ps = source; *ps != '\0';) {
for (pc = chars; *pc != '\0';) {
if (*ps == *pc)
return ps;
++pc;
}
++ps;
}
/* 没有匹配字符,返回NULL */
return NULL;
}
就是在字符串A中找到在字符串B中第一个出现的字符.
这里面pc和ps赋值的时候编译器会产生一个WARNNING.
各位看看,有啥其他好方法没.....
[[it] 本帖最后由 airhiphop 于 2009-7-24 09:42 编辑 [/it]]