tags:

views:

320

answers:

6

for example suppose that some k which is arbitrary number=2 k=2 then there is code in c

int wordcomp(char *p, char *q) {
   int n = k;
   for ( ; *p == *q ; p++, q++) {
      if (  *p==0 && --n == 0 )
         return 0;
   }
   return *p - *q;  
}      

please explain me what does this code do? also what means *p-*q?and also how implement it in java?

+1  A: 

*p-*q means subtract the value pointed to by pointer q from the value pointed to by pointer p.

this being C, subtracting 2 char values means subtracting their ASCII codes.

DVK
A: 

Returns the difference between the first two dissimilar chars in two character arrays (*p and *q) of maximum length k, i.e.

batter
batsman

Returns 't'-'s'

If both strings are same for length k and we reach the null character of p, we return 0. I think it should be || not && since these are termination conditions.

Jacob
And it looks like `k` is the number of consecutive null-terminated strings stored in `p` and `q`.
Shtééf
I think it's `min(strlen(p),strlen(q))`
Jacob
@Shteef - no, p and q are not arrays of strings. They are character arrays.
Jordan Lewis
@Jordan Lewis: When `p` hits the null terminator, `n` is decremented and the loop continues, until `n` reaches 0. Or am I missing something?
Shtééf
Jacob
Not necessarily. n is decremented only if *p==0, but it is probably by design. It's pretty obfuscated but there were worse things people came up with. So n stores the number of consecutive null terminated strings.
Maciej Hehl
+1  A: 

This function loops through two character arrays, comparing them for equality.

If they are unequal, it returns *p - *q, which is the difference between the values in the first unequal position. If, after the kth zero in the first string, there is still no inequality, then it returns 0.

So it returns the difference between the values in the first unequal position, or 0 if the first string has k zeros and is equal until that point to the second string.

Jordan Lewis
"If the kth character [of the first argument] is equal to 0, then it returns 0". Right. And it makes little sense, probably a bug.
leonbloy
Yeah, if this is trying to be a strcmp, it is not doing a very good job :)
Jordan Lewis
No, what it does is compare the strings until the kth zero. So if k==1 it will be the same as strcmp.
interjay
bwarner
Er, yeah, you're right.
Jordan Lewis
+3  A: 

Looks to be close to strcmp. It takes two string pointers, loops over them until it finds a character that is different, and then returns a positive value if p is alphabetically after q, a negative value if p is alphabetically before q, or 0 if they are the same. As stated by others, k looks to define the number of consecutive null-terminated strings to compare before it just returns 0.

bwarner
It is more similar to strncmp. But for some reason he initializes n variable in the function body. If he will do int wordcomp(char *p, char *q, int n) it would be the same strncmp.
Incognito
Not quite, remember n only gets decremented when it hits a null, so it is comparing n strings, not n bytes.
bwarner
+15  A: 

please explain me what does this code do?

It compares two words

also what means *p-*q?

It means, Tell me the value of char the pointer p is pointing to, and rest it to the char the pointer q is pointing to.

and also how implement it in java?

The *p-*q part? Here is it.

// char c
// char k
c - k;

If you mean the whole function, this could help you to get started:

int wordcomp( String sp, String sq) {
   int n = k; // I have no idea what is this for
   int pi = 0, qi = 0;
   for ( ; sp.charAt(pi) == sq.charAt(qi) ; pi++, qi++) {
      if (  sp.length()==pi && --n == 0 )
         return 0;
  }
  return sp.charAt(pi) - sq.charAt(qi);

You need to validate limits tough.

OscarRyz
thanks very much
I'm glad it was helpful. Now if I could only get +20 I would have the reversal badge :P
OscarRyz
A: 

It compares to char arrays (words) to the element set in k. Lets say if you have k=4 it means it will compare first 4 chars. Also I believe instead of

if (  *p==0 && --n==0)

you need

if (  *p==0 || --n==0)

And also please consider using p - q .

And the function name must be wordcomp instead of wrodcomp.

So to summarize it is something like strncmp from string.h.

Incognito