用指针寻找指定字符串Write a function to search a character pointed by a pointer,in a character sequence.Return the pointer pointing to the found character.Eg1:search for ‘C’ in “ABCDEF”,return the pointer point to ‘C’.Eg2:search

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/02 14:26:01

用指针寻找指定字符串Write a function to search a character pointed by a pointer,in a character sequence.Return the pointer pointing to the found character.Eg1:search for ‘C’ in “ABCDEF”,return the pointer point to ‘C’.Eg2:search
用指针寻找指定字符串
Write a function to search a character pointed by a pointer,in a character sequence.Return the pointer pointing to the found character.
Eg1:search for ‘C’ in “ABCDEF”,return the pointer point to ‘C’.
Eg2:search for ‘Z’ in “ABCDEF”,return a NULL pointer.
The function header is given by:
char *findC (char const *source,char const *obj);

用指针寻找指定字符串Write a function to search a character pointed by a pointer,in a character sequence.Return the pointer pointing to the found character.Eg1:search for ‘C’ in “ABCDEF”,return the pointer point to ‘C’.Eg2:search
#include "stdafx.h"
#define MAX_LEN 128
char *findC (char const *source, char const *obj)
{
char *p = (char *)source;
if(source == NULL || obj == NULL)
return NULL;
while(*p!='\0'){
if(*p == *obj)
return p;
else p++;
}
return NULL;
}
//Eg1: search for ‘C’ in “ABCDEF”, return the pointer point to ‘C’.
//Eg2: search for ‘Z’ in “ABCDEF”, return a NULL pointer.
void main()
{
char s1[]="ABCDEF";
//char c = 'C';
char c = 'Z';
char *value = NULL;
value = findC(s1,&c);
if(value!=NULL)
printf("%c\n",*value);
else
printf("NULL\n");
}