蓝桥杯 第四届C/C++预赛真题(5) 前缀判断(水题)

题目标题:前缀判断

如下的代码判断 needle_start指向的串是否为haystack_start指向的串的前缀,如不是,则返回NULL。

比如:"abcd1234" 就包含了 "abc" 为前缀

 1 char* prefix(char* haystack_start, char* needle_start)  2 {  3     char* haystack = haystack_start;  4     char* needle = needle_start;  5 

 6     

 7     while(*haystack && *needle){  8         if(______________________________) return NULL;  //填空位置

 9  } 10     

11     if(*needle) return NULL; 12     

13     return haystack_start; 14 }

请分析代码逻辑,并推测划线处的代码,通过网页提交。
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!


 

  水题。

  解答*(haystack++) != *(needle++)

  代码:

 1 #include <iostream>

 2 using namespace std;  3 char* prefix(char* haystack_start, char* needle_start)  4 {  5     char* haystack = haystack_start;  6     char* needle = needle_start;  7 

 8     

 9     while(*haystack && *needle){ 10         if( *(haystack++) != *(needle++) ) return NULL;  //填空位置

11  } 12     

13     if(*needle) return NULL; 14     

15     return haystack_start; 16 } 17 int main() 18 { 19     char a[1000],b[1000]; 20     cin>>a>>b; 21     cout<<prefix(a,b)<<endl; 22     return 0; 23 }

 

Freecode : www.cnblogs.com/yym2013

你可能感兴趣的:(c/c++)