-4
我想返回一个没有整数或数组索引的字符串中的子字符串的指针。这是我的代码。我不知道如何让它发挥作用。C找到字符串中的子字符串与指针
/*
* Return a pointer to the first character of the first occurrence of
* <substr> in the given <string> or NULL if <substr> is not a substring
* of <string>.
* Note: An empty <substr> ("") matches *any* <string> at the <string>'s
* start.
*****
* YOU MAY *NOT* USE INTEGERS OR ARRAY INDEXING.
*****
*/
char *find_substr(char *string, char* substr) {
char* first_occ = NULL;
while(*string) {
if(*string++ == *substr) {
if(first_occ == NULL){
first_occ = string;
}
char const *a = substr;
while((*string++ == *++a) && (*a)); /*empty*/
if(*a == '\0')
return first_occ;
}
}
return first_occ;
}
请注明你的具体问题。 “不起作用,为我修复”不是一个具体问题。例如,请描述你的程序目前做错了什么,你如何试图解决这个问题以及你需要什么特定的帮助。 – kaylum
我知道当一个空字符串作为substr传入时,它会返回null而不是字符串开头的指针。它也是当输入“bca”作为substr它匹配字符串“abccba” –
可能重复[重新启动while循环在c中没有整数](http://stackoverflow.com/questions/29587005/restarting-while- loop-in-c-without-Integers) –