473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
9 5240
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Markus Ernst | last post by:
Hi I noticed in some examples to the encrypt functions of the PHP manual a syntax was used for password checks such as if (strcmp($userpassword, md5($_POST)) == 0) { // do login } What is...
6
by: muser | last post by:
The following error appears: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'. I've already tried using single quotations. the header file only contains the struct contents....
3
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used...
53
by: Allan Bruce | last post by:
Hi there, I am reading a file into a char array, and I want to find if a string exists in a given line. I cant use strcmp since the line ends with '\n' and not '\0'. Is there a similar function...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
0
by: noobcprogrammer | last post by:
#include "IndexADT.h" int IndexInit(IndexADT* word) { word->head = NULL; word->wordCount = 0; return 1; } int IndexCreate(IndexADT* wordList,char* argv)
6
by: kevin | last post by:
Hi All here is a problem I just can't understand! I don't really want a work around I just want this to plainly work or atleast explained.... Thanks in advance.... Problem: strcmp(st, s) it runs...
47
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of...
37
by: Richard Heathfield | last post by:
candide said: They aren't. An array is an array. An address is a pointer value. These are not the same thing. If you mean that &array and &array are the same, they aren't. They have different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.