Bridge Cup Blue (2013 c / c ++ title Group B 5) Analyzing prefix

Copyright: For reprint contact me, illegal copying peril https://blog.csdn.net/qq_41106517/article/details/88372397

Subject description:


Topic Title: prefix judge

    The following code determines whether the string pointed needle_start prefix string haystack_start points, if not, NULL is returned.

    For example: "abcd1234" contains the "abc" as a prefix

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;
}


Analyze code logic, and speculated that crossed at the code, submitted via the web.
Note: Only the lack of code as the answer, do not fill in the extra code symbols or caption! !

solution:

#include<iostream>
using namespace std;
char* prefix(char* haystack_start, char* needle_start)
{
	char* haystack = haystack_start;
	char* needle = needle_start;

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

int main(){
	printf("%s",prefix("abcd123","abc"));
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41106517/article/details/88372397