-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) –