Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Codewars Grasshopper – Personalized Message c

I’ve started using Codewars to learn C and this problem is giving me issues. When I attempt to submit the answer I get errors against their test cases. I’m not sure what I did wrong or left out but any advice would be appreciated.

Errors:

"The expression (as strings) (greet(danielToo, "Daniel")) == ("Hello boss") is false: actual=Hello guest expected=Hello boss."

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"The expression strcmp(greet(second, secondCopy), greetChecker(second, second)) == 0 is false."

Instructions:

"Create a function that gives a personalized greeting. This function takes two parameters: name and owner."
Use conditionals to return the proper message:

name equals owner ‘Hello boss’

otherwise ‘Hello guest’

My code:

const char* greet(const char *name, const char *owner) {
  if (name == owner)
    {
      return "Hello boss";
    }
    else
    {
      return "Hello guest";
    }
  return "";
}

Test cases:

#include <criterion/criterion.h>

const char* greet(const char *name, const char *owner);

Test(ExampleTests, ShouldPassAllTheTestsProvided) {
    cr_assert_str_eq(greet("Daniel", "Daniel"), "Hello boss");
    cr_assert_str_eq(greet("Greg", "Daniel"), "Hello guest");
    static const char danielToo[] = "Daniel";
    cr_assert_str_eq(greet(danielToo, "Daniel"), "Hello boss");
    cr_assert_str_eq(greet("Cat", "Catherine"), "Hello guest", "Cat is not Catherine");
    cr_assert_str_eq(greet("Catherine", "Cat"), "Hello guest", "Caterine is not Cat");
}

>Solution :

Your checking if the two values point to the same place in memory, you should change your if condition to strcmp(name,owner) == 0. This function will return 0 if the two passed arguments are equal.
You can learn more about it here https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading