征战蓝桥 —— 2013年第四届 —— C/C++A组第5题——前缀判断
生活随笔
收集整理的這篇文章主要介紹了
征战蓝桥 —— 2013年第四届 —— C/C++A组第5题——前缀判断
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目標題:前綴判斷
如下的代碼判斷 needle_start指向的串是否為haystack_start指向的串的前綴,如不是,則返回NULL。
比如:“abcd1234” 就包含了 “abc” 為前綴
char* prefix(char* haystack_start, char* needle_start){char* haystack = haystack_start;char* needle = needle_start;while(*haystack && *needle){if(______________________________) return NULL; //填空位置}if(*needle) return NULL;return haystack_start;}請分析代碼邏輯,并推測劃線處的代碼,通過網頁提交。
注意:僅把缺少的代碼作為答案,千萬不要填寫多余的代碼、符號或說明文字!!
代碼
#include <iostream>using namespace std;/**** @param haystack_start 母串* @param needle_start 前綴* @return*/ char *prefix(char *haystack_start, char *needle_start) {char *haystack = haystack_start;char *needle = needle_start;//前綴while (*haystack && *needle) {//兩個指針都沒有越界 // if(______________________________) return NULL; //填空位置 //移動指針 //并判斷if (*(haystack++) != *(needle++))return NULL;}if (*needle) return NULL;return haystack_start; }int main(int argc, const char *argv[]) {cout << prefix("abcd123", "abd") << endl;return 0; }總結
以上是生活随笔為你收集整理的征战蓝桥 —— 2013年第四届 —— C/C++A组第5题——前缀判断的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 征战蓝桥 —— 2013年第四届 ——
- 下一篇: 征战蓝桥 —— 2013年第四届 ——