#include <stdlib.h> #include <string.h> char *fanis(const char *input, int value) { char *r = malloc(strlen(input) + 1), *p = r; while (*input) *p++ = *input++ + value; return *p = '\0', r; }
- #include <stdlib.h>
- #include <string.h>
char *fanis(char *r, int k) {char *w = malloc(strlen(r) + 1), *p = w;while(w && *r) *p++ = k + *r++;return w;- char *fanis(const char *input, int value)
- {
- char *r = malloc(strlen(input) + 1), *p = r;
- while (*input) *p++ = *input++ + value;
- return *p = '\0', r;
- }
#include <criterion/criterion.h> char *fanis(const char *input, int value); void basic_test(const char *input, int value, const char *expected) { char *submited = fanis(input, value); cr_assert_str_eq(submited, expected); free(submited); } Test(the_multiply_function, should_pass_all_the_tests_provided) { basic_test("123", 2, "345"); basic_test("472", 1, "583"); }
- #include <criterion/criterion.h>
- char *fanis(const char *input, int value);
Test(the_multiply_function, should_pass_all_the_tests_provided) {cr_assert_str_eq(fanis("123", 2), "345");- void basic_test(const char *input, int value, const char *expected)
- {
- char *submited = fanis(input, value);
- cr_assert_str_eq(submited, expected);
- free(submited);
- }
- Test(the_multiply_function, should_pass_all_the_tests_provided)
- {
- basic_test("123", 2, "345");
- basic_test("472", 1, "583");
- }
#include <stdlib.h> #include <string.h> char *disemvowel(char *string) { char *res = (char*)malloc(strlen(string)), *s = res; for (; *string != '\0'; string++) if (!strchr("aeiouAEIOU", *string)) *res++ = *string; *res = '\0'; return s; }
def disemvowel(string_):vowels = ["a", "e", "i", "o", "u"]new_txt = ""for i in range(len(string_)):if string_[i].lower() in vowels:continueelse:new_txt += string_[i]return new_txt- #include <stdlib.h>
- #include <string.h>
- char *disemvowel(char *string)
- {
- char *res = (char*)malloc(strlen(string)), *s = res;
- for (; *string != '\0'; string++) if (!strchr("aeiouAEIOU", *string)) *res++ = *string;
- *res = '\0';
- return s;
- }
#include <criterion/criterion.h> #include <stdlib.h> #include <string.h> char* disemvowel(char *string); void tester(char *input, const char *exp) { char *result = disemvowel(input); cr_assert_str_eq(result, exp, "Expected '%s', but got '%s'.", exp, result); free(result); } Test(remove_string, removes_vowels_from_a_string) { tester("hello", "hll"); tester("world", "wrld"); tester("apple", "ppl"); } Test(remove_string, handles_uppercase_and_lowercase_vowels) { tester("Hello World", "Hll Wrld"); tester("aEiOu", ""); } Test(remove_string, handles_strings_with_no_vowels) { tester("nthng hr", "nthng hr"); tester("bcdfghjklmnpqrstvwxyz", "bcdfghjklmnpqrstvwxyz"); } Test(remove_string, handles_empty_string) { tester("", ""); } Test(remove_string, handles_strings_with_only_vowels) { tester("aeiouAEIOU", ""); } Test(remove_string, handles_strings_with_special_characters) { tester("Shrek is an orge!", "Shrk s n rg!"); tester("The quick brown fox jumps over the lazy dog.", "Th qck brwn fx jmps vr th lzy dg."); }
import codewars_test as test# TODO Write testsimport solution # or from solution import example# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def hi_guys():test.assert_equals(disemvowel("HI GUYS"), "H GYS")test.assert_equals(disemvowel("AEIOU"), "")test.assert_equals(disemvowel("Shrek is an orge"), "Shrk s n rg")- #include <criterion/criterion.h>
- #include <stdlib.h>
- #include <string.h>
- char* disemvowel(char *string);
- void tester(char *input, const char *exp)
- {
- char *result = disemvowel(input);
- cr_assert_str_eq(result, exp, "Expected '%s', but got '%s'.", exp, result);
- free(result);
- }
- Test(remove_string, removes_vowels_from_a_string)
- {
- tester("hello", "hll");
- tester("world", "wrld");
- tester("apple", "ppl");
- }
- Test(remove_string, handles_uppercase_and_lowercase_vowels)
- {
- tester("Hello World", "Hll Wrld");
- tester("aEiOu", "");
- }
- Test(remove_string, handles_strings_with_no_vowels)
- {
- tester("nthng hr", "nthng hr");
- tester("bcdfghjklmnpqrstvwxyz", "bcdfghjklmnpqrstvwxyz");
- }
- Test(remove_string, handles_empty_string)
- {
- tester("", "");
- }
- Test(remove_string, handles_strings_with_only_vowels)
- {
- tester("aeiouAEIOU", "");
- }
- Test(remove_string, handles_strings_with_special_characters)
- {
- tester("Shrek is an orge!", "Shrk s n rg!");
- tester("The quick brown fox jumps over the lazy dog.", "Th qck brwn fx jmps vr th lzy dg.");
- }
int findMissingNumber(int arr[], int n) { int exp = (n + 2) * (n + 1) / 2, act = 0; for (int i = 0; i < n; i++) act += arr[i]; return exp - act; }
class Solution {public:int missingNumber(vector<int>& nums) {}};- int findMissingNumber(int arr[], int n)
- {
- int exp = (n + 2) * (n + 1) / 2, act = 0;
- for (int i = 0; i < n; i++) act += arr[i];
- return exp - act;
- }
#include <criterion/criterion.h> // Function to find the missing number in an array of integers int findMissingNumber(int arr[], int n); Test(findMissingNumber, should_return_5_for_given_array) { int arr[] = {1, 2, 3, 4, 6, 7, 8, 9, 10}; int n = sizeof(arr) / sizeof(arr[0]); cr_assert_eq(findMissingNumber(arr, n), 5); } Test(findMissingNumber, should_return_2_for_given_array) { int arr[] = {1, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cr_assert_eq(findMissingNumber(arr, n), 2); }
- #include <criterion/criterion.h>
- // Function to find the missing number in an array of integers
int findMissingNumber(int arr[], int n) {// Code to find the missing number}- int findMissingNumber(int arr[], int n);
Test(findMissingNumber, should_return_5_for_given_array) {int arr[] = {1, 2, 3, 4, 6, 7, 8, 9, 10};int n = sizeof(arr) / sizeof(arr[0]);cr_assert_eq(findMissingNumber(arr, n), 5);- Test(findMissingNumber, should_return_5_for_given_array)
- {
- int arr[] = {1, 2, 3, 4, 6, 7, 8, 9, 10};
- int n = sizeof(arr) / sizeof(arr[0]);
- cr_assert_eq(findMissingNumber(arr, n), 5);
- }
Test(findMissingNumber, should_return_2_for_given_array) {int arr[] = {1, 3, 4, 5};int n = sizeof(arr) / sizeof(arr[0]);cr_assert_eq(findMissingNumber(arr, n), 2);- Test(findMissingNumber, should_return_2_for_given_array)
- {
- int arr[] = {1, 3, 4, 5};
- int n = sizeof(arr) / sizeof(arr[0]);
- cr_assert_eq(findMissingNumber(arr, n), 2);
- }
The hack has been banished.
#include <stdlib.h> #include <string.h> /* Heap allocate the string and return it. */ char* remove_string(const char *input, const int n) { if (n <= 0 || input == NULL) return NULL; for (size_t i = 0, len = 0; i < (size_t)n; i++) { input += len; if ((len = strlen(input) + 1) == 1) return NULL; } return strdup(input); }
- #include <stdlib.h>
- #include <string.h>
- /*
- Heap allocate the string and return it.
- */
- char* remove_string(const char *input, const int n)
- {
- if (n <= 0 || input == NULL) return NULL;
// This is a BIG hack, and I am unsure why it works since in an edge// situation where "n" is greater than the number of '\0' in the string,// the pointer will wander into unallocated memory.for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c));char* res = strdup(input);return *res == NULL ? NULL : res;- for (size_t i = 0, len = 0; i < (size_t)n; i++)
- {
- input += len;
- if ((len = strlen(input) + 1) == 1) return NULL;
- }
- return strdup(input);
- }
Only God knows the sins that I have committed by writting this code.
#include <stdlib.h> #include <string.h> /* Heap allocate the string and return it. */ char* remove_string(const char *input, const int n) { if (n <= 0 || input == NULL) return NULL; // This is a BIG hack, and I am unsure why it works since in an edge // situation where "n" is greater than the number of '\0' in the string, // the pointer will wander into unallocated memory. for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c)); char* res = strdup(input); return *res == NULL ? NULL : res; }
- #include <stdlib.h>
- #include <string.h>
- /*
- Heap allocate the string and return it.
- */
- char* remove_string(const char *input, const int n)
- {
- if (n <= 0 || input == NULL) return NULL;
size_t len = 0;for (size_t i = 0; i < (size_t)n; ++i){input += len;len = strlen(input) + 1;if (len == 1) return NULL;}size_t index = 0;char *res = (char*)malloc(sizeof(char) * (len + 1));for (; *input != '\0'; input++) res[index++] = *input; res[index] = '\0';return res;- // This is a BIG hack, and I am unsure why it works since in an edge
- // situation where "n" is greater than the number of '\0' in the string,
- // the pointer will wander into unallocated memory.
- for (int c = 1; c < n; input++, (*input == '\0' ? c++ && input++ : c));
- char* res = strdup(input);
- return *res == NULL ? NULL : res;
- }
#include <criterion/criterion.h> #include <stdlib.h> #include <string.h> char* remove_string(const char *input, const int n); void tester(const char *input, size_t index, const char *exp) { char *result = remove_string(input, index); if (exp == NULL) cr_assert_eq(result, NULL, "Expected NULL but got %s.", result); else cr_assert_str_eq(result, exp, "Expected '%s' but got '%s'.", exp, result); free(result); } Test(remove_string, should_pass_all_the_tests_provided) { tester("hello\0world\0", 1, "hello"); tester("well\0done\0", 2, "done"); tester("you\0are\0close\0", 3, "close"); tester("you\0almost\0finished\0the\0kumite\0", 5, "kumite"); tester("not\0the\0last\0string\0", 3, "last"); tester("edge\0case\0", 0, NULL); tester("another\0edge\0case\0", 4, NULL); }
- #include <criterion/criterion.h>
- #include <stdlib.h>
- #include <string.h>
- char* remove_string(const char *input, const int n);
Test(remove_string, should_pass_all_the_tests_provided)- void tester(const char *input, size_t index, const char *exp)
- {
char* result = remove_string("hello\0world\0", 1);cr_assert_str_eq(result, "hello");free(result);result = remove_string("well\0done\0", 2);cr_assert_str_eq(result, "done");free(result);result = remove_string("you\0are\0close\0", 3);cr_assert_str_eq(result, "close");free(result);result = remove_string("you\0almost\0finished\0the\0kumite\0", 5);cr_assert_str_eq(result, "kumite");free(result);result = remove_string("not\0the\0last\0string\0", 3);cr_assert_str_eq(result, "last");free(result);result = remove_string("edge\0case\0", 0);cr_assert_eq(result, NULL);free(result);result = remove_string("another\0edge\0case\0", 4);cr_assert_eq(result, NULL);- char *result = remove_string(input, index);
- if (exp == NULL) cr_assert_eq(result, NULL, "Expected NULL but got %s.", result);
- else cr_assert_str_eq(result, exp, "Expected '%s' but got '%s'.", exp, result);
- free(result);
- }
- Test(remove_string, should_pass_all_the_tests_provided)
- {
- tester("hello\0world\0", 1, "hello");
- tester("well\0done\0", 2, "done");
- tester("you\0are\0close\0", 3, "close");
- tester("you\0almost\0finished\0the\0kumite\0", 5, "kumite");
- tester("not\0the\0last\0string\0", 3, "last");
- tester("edge\0case\0", 0, NULL);
- tester("another\0edge\0case\0", 4, NULL);
- }
This challenge involves removing the n-th string from a character array labeled "input" and then returning it. It is important to consider edge cases where the value of "n" can be zero or greater than the total number of strings available. In such cases, the function should return NULL.
#include <stdlib.h>
#include <string.h>
/*
Heap allocate the string and return it.
*/
char* remove_string(const char *input, const int n)
{
if (n <= 0 || input == NULL) return NULL;
size_t len = 0;
for (size_t i = 0; i < (size_t)n; ++i)
{
input += len;
len = strlen(input) + 1;
if (len == 1) return NULL;
}
size_t index = 0;
char *res = (char*)malloc(sizeof(char) * (len + 1));
for (; *input != '\0'; input++) res[index++] = *input; res[index] = '\0';
return res;
}
#include <criterion/criterion.h>
#include <stdlib.h>
#include <string.h>
char* remove_string(const char *input, const int n);
Test(remove_string, should_pass_all_the_tests_provided)
{
char* result = remove_string("hello\0world\0", 1);
cr_assert_str_eq(result, "hello");
free(result);
result = remove_string("well\0done\0", 2);
cr_assert_str_eq(result, "done");
free(result);
result = remove_string("you\0are\0close\0", 3);
cr_assert_str_eq(result, "close");
free(result);
result = remove_string("you\0almost\0finished\0the\0kumite\0", 5);
cr_assert_str_eq(result, "kumite");
free(result);
result = remove_string("not\0the\0last\0string\0", 3);
cr_assert_str_eq(result, "last");
free(result);
result = remove_string("edge\0case\0", 0);
cr_assert_eq(result, NULL);
free(result);
result = remove_string("another\0edge\0case\0", 4);
cr_assert_eq(result, NULL);
free(result);
}
Just use 'stdint.h' :D
One liner.
#include <stdio.h> int prnt_mltply(int m, int n) { for (int i = 0; i < m; i++) for (int j = 1; j <= n; j++) printf("%d * %d = %d\n", i + 1, j, (i+1) * j); return m * n; }
- #include <stdio.h>
- int prnt_mltply(int m, int n) {
int i = 1;while(i <= m) {int j = 1;while(j <= n) {printf("%d * %d = %d", i, j, i * j);j++;}i++;printf("\n");}- for (int i = 0; i < m; i++) for (int j = 1; j <= n; j++) printf("%d * %d = %d
- ", i + 1, j, (i+1) * j);
- return m * n;
- }