Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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
0
deta_gen
Asked:
deta_gen
1 Solution
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now