Ad
Code
Diff
  • unsigned char invertbits(unsigned char value) { return ~value; }
    • unsigned char invertbits(unsigned char value)
    • {
    • return value ^ 0xFF;
    • }
    • unsigned char invertbits(unsigned char value) { return ~value; }
Code
Diff
  • int just_why(int a, int b) { return b ? a + just_why(a, b - 1) : 0; }
    int multiply(int a, int b) { return just_why(a, b); }
    • int multiply(int a, int b) {
    • return a * b;
    • }
    • int just_why(int a, int b) { return b ? a + just_why(a, b - 1) : 0; }
    • int multiply(int a, int b) { return just_why(a, b); }
Test Cases
Diff
  • // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
    
    #include <criterion/criterion.h>
    
    // replace with the actual method being tested
    int multiply(int a,int b);
    
    Test(the_multiply_function, should_pass_all_the_tests_provided) {
        cr_assert_eq(multiply(1, 1), 1);
        cr_assert_eq(multiply(2, 2), 4);
        cr_assert_eq(multiply(3, 7), 21);
    }
    
    • // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
    • #include <criterion/criterion.h>
    • // replace with the actual method being tested
    • int multiply(int a,int b);
    • Test(the_multiply_function, should_pass_all_the_tests_provided) {
    • cr_assert_eq(multiply(1, 1), 1);
    • cr_assert_eq(multiply(2, 2), 4);
    • cr_assert_eq(multiply(3, 7), 21);
    • }
Code
Diff
  • const char *strpbrknul(const char *s, const char *accept) 
    {
    	char *character[256] = { 0 };
    	while (*accept)
    	{
    		character[*accept++]++;
    	}
    	while (*s)
    	{
    		if (character[*s])
    		{
    			return s;
    		}
    		s++;
    	}
    	return s;
    }
    • #include <string.h>
    • const char* strpbrknul(const char* s, const char* accept){
    • for(; *s; ++s)
    • if(strchr(accept, *s)) return s;
    • const char *strpbrknul(const char *s, const char *accept)
    • {
    • char *character[256] = { 0 };
    • while (*accept)
    • {
    • character[*accept++]++;
    • }
    • while (*s)
    • {
    • if (character[*s])
    • {
    • return s;
    • }
    • s++;
    • }
    • return s;
Code
Diff
  • #include<stdbool.h>
    
    const char *bool_to_word (bool value)
    {
        return value ? "Yes" : "No";
    }
    • #include<stdbool.h>
    • const char *bool_to_word (int value)
    • const char *bool_to_word (bool value)
    • {
    • if (value == 0)
    • return "No";
    • else if (value!=0)
    • return "Yes";
    • return value ? "Yes" : "No";
    • }
Algorithms
Code
Diff
  • #include <stdint.h>
    
    // naive implementation
    uint8_t number_of_digits(uint64_t n)
    {
    	uint8_t digits = 0;
    	do
    	{
    		n /= 10;
    		digits++;
    	} while (n > 0);
    	return digits;
    }
    • #include <math.h>
    • #include <stdint.h>
    • // naive implementation
    • uint8_t number_of_digits(uint64_t n)
    • {
    • return log10(n) + 1;
    • uint8_t digits = 0;
    • do
    • {
    • n /= 10;
    • digits++;
    • } while (n > 0);
    • return digits;
    • }
Code
Diff
  • int reverse_int(int num)
    {
        int rev = 0;
        while (num > 9 || -9 >= num)
        {
            rev += num % 10;
            rev *= 10;
            num /= 10;
        }
        return rev + num;
    }
    • int reverse_int_recc(int num, int rev) {
    • return num ? reverse_int_recc(num / 10, rev * 10 + (num % 10)): rev;
    • }
    • int reverse_int(int num) {
    • return reverse_int_recc(num, 0);
    • }
    • int reverse_int(int num)
    • {
    • int rev = 0;
    • while (num > 9 || -9 >= num)
    • {
    • rev += num % 10;
    • rev *= 10;
    • num /= 10;
    • }
    • return rev + num;
    • }