#include <stdlib.h> #include <string.h> char *reverse_string(const char *word) { if (!word) return NULL; size_t length = strlen(word); char *result = malloc(length + 1); if (!result) return NULL; for (size_t i = 0; i < length; i++) result[i] = word[length - i - 1]; result[length] = '\0'; return result; }
- #include <stdlib.h>
- #include <string.h>
char *reverse_string(const char *word){- char *reverse_string(const char *word) {
- if (!word)
- return NULL;
char *res = malloc(strlen(word) + 1);for(int i = strlen(word) ; i>=0 ; res[i] = word[strlen(word) - (--i) - 1]);res[strlen(word)] = '\0';return res;}- size_t length = strlen(word);
- char *result = malloc(length + 1);
- if (!result)
- return NULL;
- for (size_t i = 0; i < length; i++)
- result[i] = word[length - i - 1];
- result[length] = '\0';
- return result;
- }
#include <stdlib.h> #include <string.h> #include <stdio.h> void append_number_from_0_to_99_as_text(char *string, unsigned value) { static char *from_0_to_19_as_text[] = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN", }; static char *tens_digit_to_ty_form[] = { "", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"}; if (value > 19) { strcat(string, tens_digit_to_ty_form[value / 10]); if (value % 10) { strcat(string, " "); strcat(string, from_0_to_19_as_text[value % 10]); } } else strcat(string, from_0_to_19_as_text[value]); } char *checkAmount(double amount) { if (amount >= 10000) return NULL; char *buffer = (char[64]) { 0 }; unsigned dollars = (unsigned)amount; unsigned cents = (unsigned)(amount * 100) % 100; append_number_from_0_to_99_as_text(buffer, dollars); strcat(buffer, " and "); char *tiny_buffer = &(char[8]) { 0 }; sprintf(tiny_buffer, "%d", cents); strcat(buffer, tiny_buffer); strcat(buffer, "/100"); return strdup(buffer); }
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
#include <stdlib.h>- void append_number_from_0_to_99_as_text(char *string, unsigned value) {
- static char *from_0_to_19_as_text[] = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN", };
- static char *tens_digit_to_ty_form[] = { "", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"};
- if (value > 19) {
- strcat(string, tens_digit_to_ty_form[value / 10]);
- if (value % 10) {
- strcat(string, " ");
- strcat(string, from_0_to_19_as_text[value % 10]);
- }
- }
- else
- strcat(string, from_0_to_19_as_text[value]);
- }
- char *checkAmount(double amount) {
char * return_val = NULL;// Your code here!return return_val;- if (amount >= 10000)
- return NULL;
- char *buffer = (char[64]) { 0 };
- unsigned dollars = (unsigned)amount;
- unsigned cents = (unsigned)(amount * 100) % 100;
- append_number_from_0_to_99_as_text(buffer, dollars);
- strcat(buffer, " and ");
- char *tiny_buffer = &(char[8]) { 0 };
- sprintf(tiny_buffer, "%d", cents);
- strcat(buffer, tiny_buffer);
- strcat(buffer, "/100");
- return strdup(buffer);
- }
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); }
// 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);
- }
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;
Algorithms
#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;
- }
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;
- }