Ad
Code
Diff
  • #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:
    • continue
    • else:
    • 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;
    • }
Code
Diff
  • 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;
    • }
Algorithms
Strings

The hack has been banished.

Code
Diff
  • #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);
    • }
Algorithms
Strings

Only God knows the sins that I have committed by writting this code.

Code
Diff
  • #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;
    • }
Algorithms
Strings

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;
}
Algorithms

Just use 'stdint.h' :D

Code
Diff
  • #include <stdint.h>
    number_of_digits(uint64_t n){uint8_t d=0;for(;n!=0;n/=10)d+=1;return d;}
    • #include <math.h>
    • #include <stdint.h>
    • uint8_t number_of_digits(uint64_t n)
    • {
    • return log10(n) + 1;
    • }
    • number_of_digits(uint64_t n){uint8_t d=0;for(;n!=0;n/=10)d+=1;return d;}

One liner.

Code
Diff
  • #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;
    • }