开发者

Java | compare char word in a char array

How do I get the index of a single word (represents in a char array) which c开发者_如何学Goan be found in a paragraph (again represents in a char array).

the char represents the word

char word[] = new char[]{'w','o','r','d'};

and here's the paragraph

char para[] = new char[]{'f','g','q','z','y','i','o','p','w','o','r','d'};

I would like to get the index of the first letter in this case 8th. I used binary search by when sorting the words get scrambled.

Thanks.


A bit inefficient theoretically, but rather practical and simple:

int position = new String(paragraph).indexOf(new String(word));

If you want to understand how this works - check the static int indexOf(..) method of java.lang.String


Binary search won't help you in this case. You have to search linearly. The easiest solution would be to search linearly for the first character and, when found, check if the remaining word follows.

A more elaborate solution would be to use a KMP algorithm.


The simplest method is just to try all possibilities, by looping through each starting point and testing if all characters match. By the fact you've already mentioned binary search, this is probably simple enough for you to already know, though let me know if that's what you're looking for.

If you're looking for the best method, see http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm .


You can convert the character arrays to strings. The result of the search in the string is the same as if you had searched the arrays.

String needle = new String(word);
String haystack = new String(para);
int i = haystack.indexOf(needle);

Result:

8

This can be much faster than a naive O(n*m) search because the string function indexOf is optimized.

If you want to do it without creating the temporary strings you can implement a string searching algorithm for byte arrays. You could for example choose the Boyer-Moore algorithm which has worst case O(n).


Quick answer and I suppose others will be more elaborate. Initially, I'd do something like this (pseudocode is better for thinking out algorithms):

boolean nonmatchingchar
integer i, j
for each i of word until endof word
    for each j of para until endof para
      if word i isnotequalto para i set nonmatchingchar true     
    end for
end for


if nonmatchingchar is true print "character sequence not found"

Edit: To make this more efficient in the case where you'd have several words to search for, you could constitute a two-dimensional array with words sorted according to their first letter. From there on, you could go through the second array letter by letter and test a subset of words according to that letter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜