?
Solved

easy question newbie

Posted on 2003-02-25
6
Medium Priority
?
312 Views
Last Modified: 2008-02-01
printf("%s \n", (0 == strequiv(a, b)) ? "Equivalent" : "Not Equivalent");

What does the above do?  And how do i modify it it so that
if strequiv is true it returns 1;

for your information, here is strequiv:

int strequiv(const char *a, const char *b)
{
 char hashPresenceA[256];
 char hashPresenceB[256];

 calcHashPresence(hashPresenceA, a);
 calcHashPresence(hashPresenceB, b);

 return memcmp(hashPresenceA, hashPresenceB, sizeof(hashPresenceA));
}

int calcHashPresence(char hashPresence[256], const char *string)
{
 const char *p = string;
 unsigned int i = 0;

 memset(hashPresence, 0, 256);

 while (p && p[i])
   {
   if (isalpha(p[i]))
     {
     hashPresence[tolower(p[i])] = 1;
     }
   i++;
   }

 return 0;
 printf("\n");
}

Thanks
0
Comment
Question by:deta_gen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:Kocil
ID: 8023334
The (X ? Y : Z) mean ...
if X==true then return Y else return Z;

So the expanded syntax of
printf("%s \n", (0 == strequiv(a, b)) ? "Equivalent" : "Not Equivalent");

is:
-----------------------------
char *temp;
if (strequiv(a,b) == 0) {
  temp = "Equivalent"
}
else {
  temp = "Not Equivalent";
}

printf("%s", temp);
------------------------------

If you want the result is 1 or 0 the syntax is:
int result = strequiv(a, b) ? 1 : 0;

But since in C true==1 and false==0, then you may use the simplest:
int result = strequiv(a,b);

Regards

"Not Equivalent");
 

0
 
LVL 5

Accepted Solution

by:
Kocil earned 140 total points
ID: 8023344
Please don't read the last line thay say "Not Equivalent");.
I didn't know it was there.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 8023578
Is there a good reason you can't just use the good old-fashioned strcmp() function here?
0
 
LVL 6

Expert Comment

by:zebada
ID: 8024117
> Is there a good reason you can't just use the good old-fashioned strcmp() function here?

Well, maybe because thes strequiv() function is not doing anything like a strcmp() - nor even a stricmp().

If you want to use the following statement in your code:

printf("%s \n",strequiv(a,b)?"Equivalent":"Not Equivalent");

then you need to change the return statement of the strequiv() function to this:

return memcmp(hashPresenceA, hashPresenceB, sizeof(hashPresenceA))==0;

because memcmp returns 0 if the memory blocks are identical.

Regards
Paul

0
 
LVL 20

Expert Comment

by:jmcg
ID: 10018959
Nothing has happened on this question in over 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Kocil.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question