我遇到了一些我正在編寫的程序的問題。只有當我的子字符串在字符串末尾時,strstr纔有效
- 的strstr輸出我的子只有當它在我的字符串的結尾
- 也輸出後
- 我已經受夠了「爲const char *大海撈針」問題的一些垃圾字符,然後加入輸入到它,所以我用fgets和的getchar循環做了
- 某處它與一個子,這不是隻在年底的工作方式,但後來我輸出字符串和字符串的亞特那
這裏是我的主:
int main() {
char haystack[250],
needle[20];
int currentCharacter,
i=0;
fgets(needle,sizeof(needle),stdin); //getting my substring here (needle)
while((currentCharacter=getchar())!=EOF) //getting my string here (haystack)
{
haystack[i]=currentCharacter;
i++;
}
wordInString(haystack,needle);
return(0);
}
和我的功能:
int wordInString(const char *str, const char * wd)
{
char *ret;
ret = strstr(str,wd);
printf("The substring is: %s\n", ret);
return 0;
}
有一個終止「\ 0」大海撈針丟失,這將導致你各種各樣的麻煩。 – gnasher729