概述
以下是我的练习代码:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<assert.h>
#define MAX 100
bool find_char1(char **strings,char value);
bool find_char2(char **strings,char value);
const char * find_char(char const * source,char const * chars);
int main()
{
//char * str1="sunxiaowu";
//char *str2="xiaowu";
char str1[MAX]={0};
char str2[MAX]={0};
const char * str3;
printf("请输入两个字符串(以空格隔开)n");
scanf("%s",str1);
scanf("%s",str2);
str3=find_char(str1,str2);
if(str3)
{
printf("匹配成功,匹配的第一个字符是%cn",*str3);
}
return 0;
}
const char * find_char(char const * source,char const * chars)
{
const char * source1=source;
const char * chars1=chars;
if(source1 != NULL && source1 !="" && chars1 !=NULL && chars1 !="")
{
while (*source1)
{
while(*chars1)
{
if(*chars1==*source1)
{
return chars1;
}
++chars1;
}
chars1=chars;
++source1;
}
}
return NULL;
}
书籍上的正确答案:
char * find_char_book(char const *str,char const * chars)
{
char * cp;
if(str != NULL && chars != NULL)
{
for(; *str!="