By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

strcmp() question, 4 words, two strings, equal return value.

P: n/a
Hello,

I have a question about strcmp(). I have four words, who need to be
compared if it were two strings. I tried adding the comparison values
like '(strcmp(w1, w2) + strcmp(w3, w4))', where w1 and w2 make up the
first string and, w3 and w4 make up the second string. I do not want
to allocate memory, then put the words together to create a string
only to facilitate strcmp() comparison.

My question; Does anyone know how to get the same strcmp() return
value I would normally get when comparing the four words like if they
were two strings ?

Thank you.

Steven.
Dec 29 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi,

In a generic way, the following expression could help (not tested):

int x;
....
((x=strcmp(w1,w2))!=0)? x : strcmp(w3,w4)

However, in your question is not clear what do you expect about
different lengths of strings.

Kind regards.

Dec 29 '05 #2

P: n/a
Steven <St****@yahoo.com> wrote:
I have a question about strcmp(). I have four words, who need to be
compared if it were two strings. I tried adding the comparison values
like '(strcmp(w1, w2) + strcmp(w3, w4))',
Why were you trying that ?
where w1 and w2 make up the first string and, w3 and w4 make up the second
string. I do not want to allocate memory, then put the words together to
create a string only to facilitate strcmp() comparison.

My question; Does anyone know how to get the same strcmp() return
value I would normally get when comparing the four words like if they
were two strings ?


Just to make sure: is this a homework assignment ?

The solution should not be too complicated; start with comparing the first
halves of both strings, and figure out in which cases this first comparison
tells you enough about the two strings being the same or different, and in
which case you would have to do some more strcmp()'ing.

Ico

--
:wq
^X^Cy^K^X^C^C^C^C
Dec 29 '05 #3

P: n/a
On 29 Dec 2005 15:15:36 GMT, us****@zevv.nl wrote:
Steven <St****@yahoo.com> wrote:
I have a question about strcmp(). I have four words, who need to be
compared if it were two strings. I tried adding the comparison values
like '(strcmp(w1, w2) + strcmp(w3, w4))',
Why were you trying that ?


Because I have two binary trees. One which holds all the actual data
e.g. uniq words and their frequencies. The second binary tree consists
of nodes who hold two pointers to the word freq's. Those two words
make up a bigram.

So basicly a binary tree of pointers that is linked with another
binary tree which holds the actual data. That way only uniq words are
saved and not the whole input amount and for each word only two mem
alloc's & free's have to be made. [one for the node, one for the
word].

The only trade-off is that a seperate function has to be coded to
create & install a tree that only consists of pointers. That is why I
need that strcmp() because two pointers arrive to nodes which hold the
actual data. A normal treeadd() function, but instead of one word, two
pointers to other nodes have to be added.

I hope it is a bit clear..
where w1 and w2 make up the first string and, w3 and w4 make up the second
string. I do not want to allocate memory, then put the words together to
create a string only to facilitate strcmp() comparison.

My question; Does anyone know how to get the same strcmp() return
value I would normally get when comparing the four words like if they
were two strings ?


Just to make sure: is this a homework assignment ?


Owww. thank you for the compliment, it's been at least 20 years since
someone asked me that :-) I am going to brag about this to my wife
tonight...
The solution should not be too complicated; start with comparing the first
halves of both strings, and figure out in which cases this first comparison
tells you enough about the two strings being the same or different, and in
which case you would have to do some more strcmp()'ing.
I thought of that in the first moment of the problem. But.. You will
always have to compare all words since you never know in advance how
much both the second words from the string will differ. In my case
that would create an crippeld unbalanced tree since the strcmp() value
is used to index it.

Just comparing w1 and w2, w3 and w4; and then adding the resulting
values to come to the comaprison value unfortunatly does not work. :( Ico


J.
Dec 29 '05 #4

P: n/a
On 29 Dec 2005 07:06:34 -0800, "tmp123" <tm****@menta.net> wrote:
Hi,

In a generic way, the following expression could help (not tested):

int x;
...
((x=strcmp(w1,w2))!=0)? x : strcmp(w3,w4)

However, in your question is not clear what do you expect about
different lengths of strings.
To return the same value by strcmp() when comparing the four words as
if it were two full strings.
Kind regards.


Thank you for the quick solution. At first glance this would work,
however when the two strings are equal, it does not return zero. See
the last output.

Thanks.

Steven.

#include <stdio.h>
#include <string.h>

int mystrcmp(const char *w1, const char *w2, const char *w3, const
char *w4);

int main(void) {
int x = 0;
char *stra = "jasondonovan";
char *strb = "kristalclear";
char *w1 = "jason";
char *w2 = "donovan";
char *w3 = "kristal";
char *w4 = "clear";

printf("1. %d\n", strcmp(stra, strb));

((x = strcmp(w1, w2)) != 0) ? x : strcmp(w3, w4);

printf("2. %d\n", x);
printf("3. %d\n", mystrcmp(w1, w2, w3, w4));
printf("4. %d\n", mystrcmp("first", "second", "first", "second"));

return 0;
}

int mystrcmp(const char *w1, const char *w2, const char *w3, const
char *w4) {
int x = 0;

if((x = strcmp(w1, w2)) != 0)
return x;
else
return strcmp(w3, w4);

/* ((x = strcmp(w1, w2)) != 0) ? x : strcmp(w3, w4); */
}
Dec 29 '05 #5

P: n/a
Steven <St****@yahoo.com> wrote:
On 29 Dec 2005 15:15:36 GMT, us****@zevv.nl wrote:
Steven <St****@yahoo.com> wrote:
> I have a question about strcmp(). I have four words, who need to be
> compared if it were two strings. I tried adding the comparison values
> like '(strcmp(w1, w2) + strcmp(w3, w4))',
Why were you trying that ?
(... explanation about binary tree application ...)
I hope it is a bit clear..


A bit, yes :)

But (strcmp(w1, w2) + strcmp(w3, w4)) will not do what you want, will it ?

As far as I know strcmp() will only tell you if a string is bigger, equal or
smaller then another string by returning <0, ==0 or >0, but the magnitude
(if that's the right word) of the return value is not defined. If "foo" is
bigger then "bar", it could return +1, MAXINT or any other positive number.
I believe adding the return values of two strcmp()'s does not give
meaningful results.
> where w1 and w2 make up the first string and, w3 and w4 make up the second
> string. I do not want to allocate memory, then put the words together to
> create a string only to facilitate strcmp() comparison.
>
> My question; Does anyone know how to get the same strcmp() return
> value I would normally get when comparing the four words like if they
> were two strings ?


Just to make sure: is this a homework assignment ?


Owww. thank you for the compliment, it's been at least 20 years since
someone asked me that :-) I am going to brag about this to my wife
tonight...


Hehe, sorry for asking, but lack of context *is* suspicious, after all :)
The solution should not be too complicated; start with comparing the first
halves of both strings, and figure out in which cases this first comparison
tells you enough about the two strings being the same or different, and in
which case you would have to do some more strcmp()'ing.


I thought of that in the first moment of the problem. But.. You will
always have to compare all words since you never know in advance how
much both the second words from the string will differ.


I'm not sure if you always have to compare all words, but maybe I still
don't understand your problem thoroughly.

You have four words: w1, w2, w3, w4. Those four words make up two strings : s1
which is the concatination of w1 and w2, and s2 which is the concatination
of w3 and w4.

Compare the first two halves of s1 and s2, which would be w1 and w3. If the
lengths differ, you only strncmp() up to the length of the shortest of the
two. If these two don't match, there would be no need to do any more
comparing. If they *do* match, you would have to do more comparing after
that.
--
:wq
^X^Cy^K^X^C^C^C^C
Dec 29 '05 #6

P: n/a
Steven <St****@yahoo.com> wrote:

Thank you for the quick solution. At first glance this would work,
however when the two strings are equal, it does not return zero. See
the last output.

(...)

int mystrcmp(const char *w1, const char *w2, const char *w3, const char *w4)
{
int x = 0;

if((x = strcmp(w1, w2)) != 0)
return x;
else
return strcmp(w3, w4);

/* ((x = strcmp(w1, w2)) != 0) ? x : strcmp(w3, w4); */
}


I think things are mixed up here. Didn't you mean to compare w1 to w3 and w2
to w4 ?

--
:wq
^X^Cy^K^X^C^C^C^C
Dec 29 '05 #7

P: n/a
Hi,

As has been said by another poster, there are a small confusion on the
usage of w1,...w4.

For the naming order you give in the example, the correct expresion is:
((x = strcmp(w1, w3)) != 0) ? x : strcmp(w2, w4);

Another question is the expected answer in case of full strings are
equal, but partial not:

char *stra = "jasondonovan";
char *strb = "jasondonovan";
char *w1 = "jason";
char *w2 = "donovan";
char *w3 = "jasondo";
char *w4 = "novan";

Kind regards.

Dec 29 '05 #8

P: n/a
Steven wrote:

I have a question about strcmp(). I have four words, who need to
be compared if it were two strings. I tried adding the
comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))',
where w1 and w2 make up the first string and, w3 and w4 make up
the second string. I do not want to allocate memory, then put
the words together to create a string only to facilitate
strcmp() comparison.

My question; Does anyone know how to get the same strcmp()
return value I would normally get when comparing the four words
like if they were two strings ?


#include <string.h>

int twowordcompare(char *w1, char *w2, /* 1st pair */
char *w3, char *w4) /* 2nd pair */
{
int result;

if (0 == (result = strcmp(w1, w3)))
result = strcmp(w2, w4));
return result;
}

(which assumes the compound comparands are always split in the same
places).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Dec 29 '05 #9

P: n/a
Steven <St****@yahoo.com> writes:
I have a question about strcmp(). I have four words, who need to be
compared if it were two strings. I tried adding the comparison values
like '(strcmp(w1, w2) + strcmp(w3, w4))', where w1 and w2 make up the
first string and, w3 and w4 make up the second string. I do not want
to allocate memory, then put the words together to create a string
only to facilitate strcmp() comparison.

My question; Does anyone know how to get the same strcmp() return
value I would normally get when comparing the four words like if they
were two strings ?


You need to define the problem more precisely. You say you have "four
words"; I presume you have a pointer to each word, which is stored as
a string (w1, w2, w3, w4), so strcmp(w1, w2) would compare the first
and second words. So far, that's clear enough.

You then say you need to compare the four words "as if it were two
strings". Well, they're not two strings, they're four strings. There
are a multitude of ways you could treat them "as if" they were two
strings.

The first thing you should do is define precisely, in English, just
what you're looking for. Once you've done that, you can nail down
your definition by writing something in C that's functionally what you
want, but not necessarily as efficient as you want it.

Here's an example of what you *might* mean:

int compare(char *w1, char *w2, char *w3, char *w4)
{
char *s1 = malloc(strlen(w1) + strlen(w2) + 1);
char *s2 = malloc(strlen(w3) + strlen(w4) + 1);
int result;
if (s1 == NULL || s2 == NULL) {
/* Insert error handling here. */
}
strcpy(s1, w1);
strcat(s1, w2);
strcpy(s2, w3);
strcat(s2, w4);
result = strcmp(s1, s2);
free(s1);
free(s2);
return result;
}

If C supported "+" as a string concatenation operator, this would be
equivalent to strcmp(w1+w2, w3+w4). That may or may not be what you
want. Since you refer to the input strings as "words", it might also
make sense to concatenate them together with a space between them.

Finally, *why* don't you want to "allocate then put the words together
to create a string only to facilitate strcmp() comparison"? If I
understand your problem statement correctly, that seems like the most
obvious way to approach the problem. If you're worried about
efficiency, and you'd rather compare the strings in place, that's
reasonable, but you should consider getting something working first,
then worry later about making it more efficient. You might find that
the allocation isn't a bottleneck, and optimizing that code might not
be worth the effort. On the other hand, if that is a bottleneck,
there are probably ways to write a strcmp() replacement that traverses
pairs of strings rather than single strings.

Describe more precisely what you're trying to do, and perhaps we can
help you do it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 29 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.