• C

easy question newbie

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
deta_genAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KocilCommented:
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
KocilCommented:
Please don't read the last line thay say "Not Equivalent");.
I didn't know it was there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pjknibbsCommented:
Is there a good reason you can't just use the good old-fashioned strcmp() function here?
0
zebadaCommented:
> 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
jmcgOwnerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.