字串問題 返回乙個字串的指定子串

2022-04-10 00:47:31 字數 1982 閱讀 4886

2013-09-15 11:06:57

模擬c++中的strstr函式,將主串中子串以及以後的字串全部返回。

注意:考慮所有可能的測試用例,包括子串存在、不存在,以及子串、主串為空的情況等。

1 #include 2 #include 3

using

namespace

std;45

char *mystrstr(const

char* psrcstr,const

char*psubstr)613

14char *psrccur = (char *)psrcstr;

15char *psubcur =null;

16char *psrctmp =null;

1718

while (*psrccur)

1928

29if ( !(*psubcur) ) //

子串的所有字元都測試通過,說明找到子串,返回起始位置即可

3033

34 ++psrccur;35}

3637

return null; //

沒有找到,返回null38}

3940 typedef char *pchar;

4142

void

testdriver()43;

45 pchar substrarray = ;

46 size_t arraylength = 7;47

48pchar srcstr;

49pchar substr;

5051

pchar psubpos;

5253

for (size_t index = 0;index < arraylength;++index)

5466

else

6770}71

}7273int

main()

74

測試結果:

the source string

is : 0123456

the sub

string

is : 123

the sub

string

is : 123456

the source

string

is : 0123456

the sub

string

is : 45678

the sub

string

is not found!the source

string

is: yyabcdabjcabceg

the sub

string

is: abc

the sub

string

is: abcdabjcabceg

the source

string

is: abcbcbcabc

the sub

string

is: hello

the sub

string

is not found!the source

string

is: abc

the sub

stringis:

the sub

string

is not found!the source

stringis:

the sub

stringis:

the sub

string

is not found!the source

stringis:

the sub

string

is: abc

the sub

string

is not found!請按任意鍵繼續. . .

字串問題 返回乙個字串的指定子串

2013 09 15 11 06 57 模擬c 中的strstr函式,將主串中子串以及以後的字串全部返回。注意 考慮所有可能的測試用例,包括子串存在 不存在,以及子串 主串為空的情況等。1 include 2 include 3 using namespace std 45 char mystrst...

字串處理 乙個字串包含另乙個字串的所有字元

假設這有乙個各種字母組成的字串,假設這還有另外乙個字串,而且這個字串裡的字母數相對少一些。從演算法是講,什麼方法能最快的查出所有小字串裡的字母在大字串裡都有?比如,如果是下面兩個字串 string 1 abcdefghlmnopqrs string 2 dcgsrqpom 答案是true,所有在st...

乙個字串是否包含另乙個字串的問題

string s1 2,3,4,5,11,string s2 1,2個字串s1和s2,我用什麼方法判斷s2在不在s1裡呢,而不是把s1裡的 11,當成s2 using system using system.text.regularexpressions class test 在這個程式中,s1 和...