tags:

views:

286

answers:

5

Suppose we have the following method (it is in c code):

const char *bitap_search(const char *text, const char *pattern)

My question is how can I compare text and pattern if they are char? This method is like a substring problem but I am confused a bit can I write in term of char such code? if (text[i]==pattern[i])? look i am interesting at this algorithm in java http://en.wikipedia.org/wiki/Bitap_algorithm how implement this in java?

R = malloc((k+1) * sizeof *R);

and please help me to translate this code in java so we have two string text? like "i like computer it is very important" and patter string " computer it is very"? can anybody explain me what we have instead of char?

A: 

You probably need strcmp() or strpos().

grawity
+3  A: 

I'm not sure what exactly you are asking, but if you mean to find pattern in text, then strstr(text, pattern). Or if you mean to just compare text and pattern, then strcmp(text, pattern) (note that it returns 0 when text and pattern are equal).

Edit based on discussion in comments: If you mean to ask how to implement the indexing of individual characters in Java, then substitute (in Java) text.charAt(i) for the C text[i]. In C the chars in strings can be indexed directly like an array, in Java one needs to call the correct method in String.

Edit 2: The C code const char * can be replaced in Java with String.

In C malloc is used to allocate memory; in this case it allocates room in the array R for m+1 elements. So, the BIT *R can be removed and R = malloc((m+1) * sizeof *R); replaced with boolean[] R = new boolean[m + 1];. When assigning values into the array R substitute true for 1 and false for 0.

Arkku
http://en.wikipedia.org/wiki/Bitap_algorithm look i am asking about this
@davit-datuashvili: Umm, that Wikipedia page already contains a C implementation of this algorithm. Are you confused about some aspect of that code? If so, please edit your question with to ask about that specifically (and link to the Wikipedia page from there).
Arkku
i am planning to implement in java how rite this? R = malloc((k+1) * sizeof *R);?
Please edit that into the actual question at the top; as you can see most people trying to answer you have no idea what you actually mean to ask.
Arkku
A: 

You should use strncmp(). The syntax is something like:

int strncmp( const char *str1, const char *str2, size_t count );

It is the best and more secure way of comparing strings, but of course you will need to know their length, or at least the minimum length between them.

Freddy
if (pattern[0] == '\0') it means that pattern.length==0 yes? because it is at the begining of code
+2  A: 

I think you are confused about the difference between char and char *. In C there is no built-in string type. Strings are represented as null-terminated character arrays, meaning that the last character of the string must be \0 So char is a single character, while char * is a pointer to an array of characters, i. e. a string. And that means that it is perfectly fine to say if (text[i] == pattern[i]).

Dima
+2  A: 

You might try these:

CPerkins