Some problems of STRSTR function implemented by Java

Source: Internet
Author: User

The implementation process in Java is as follows:

1  Public Static intStrstrChar[] CArray1,Char[] cArray2) {2     if(carray1!=NULL&& carray2!=NULL 3&&!carray1.equals ("") &&!carray2.equals ("")4&& carray1.length>=carray2.length) {5         intM=0;6          for(inti=0;i<=carray1.length-carray2.length;i++){7             if(carray1[i]==Carray2[m]) {8m++;9                 if(m==carray2.length) {Ten                     returnI-carray2.length+1; One                 } A                 Continue; -}Else{ -M=0; the             } -         } -     } -     return-1;  +}

This is a classmate interview C out of a question, require C implementation.

Java implementation issues:

The length property is not allowed. As the development of library functions, these properties are not allowed here.

====

The C language can be manipulated by pointers.

The string in C ends with ' + ' and moves the pointer to see what it refers to.

If CArray2 is empty first, or cArray2 and cArray1 are empty at the same time, it is included; if CArray1 is empty first, it is not included.

=====

To implement a C-pointer-like function in Java, think of:

1, to capture the way the bounds of the anomaly as an array to end the judgment ...

2, add a symbol such as ' # ' to the end, as the end of the flag. But what if the array itself contains: Seemingly no solution.

=========================================

In addition to these, the idea of Java implementation is as follows:

1, traverse cArray1;

2, set the variable m to record the cArray2 position.

The initial value is 0, then the bitwise comparison, and if there are unequal conditions, m resets to 0. CArray1 then traverses backwards, but cArray2 starts from the beginning.

3. Determine the conditions for success. M==carray2.length.

-------

Also, the value of the char type is compared for equality, directly with "= =" because it is the underlying variable ....

Some problems of STRSTR function implemented by Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.