用指针寻找指定字符串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/06/25 20:11:11
![用指针寻找指定字符串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](/uploads/image/z/11558428-52-8.jpg?t=%E7%94%A8%E6%8C%87%E9%92%88%E5%AF%BB%E6%89%BE%E6%8C%87%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2Write+a+function+to+search+a+character+pointed+by+a+pointer%2Cin+a+character+sequence.Return+the+pointer+pointing+to+the+found+character.Eg1%3Asearch+for+%E2%80%98C%E2%80%99+in+%E2%80%9CABCDEF%E2%80%9D%2Creturn+the+pointer+point+to+%E2%80%98C%E2%80%99.Eg2%3Asearch)
用指针寻找指定字符串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");
}