How to Determine if a String Only Contains Unique Characters in C

The challenge Write a program to determine if a string contains only unique characters. Return true if it does and false otherwise. The string may contain any of the 128 ASCII characters. Characters are case-sensitive, e.g. ‘a’ and ‘A’ are considered different characters. The solution in C Option 1: int has_unique_chars(const char *str) { int mask[128]={0}; while (*str) if (++mask[*str++]>1) return 0; return 1; } Option 2: #include <limits.h> _Bool has_unique_chars(const char *str) { char hit[CHAR_MAX + 1] = {0}; while (*str) { if (*str < 0) { str++; continue; } if (hit[*str]) return 0; hit[*str++] = 1; } return 1; } Option 3:...

November 30, 2022 · 1 min · 184 words · Andrew

How to Find the Maximum Multiple in C

The challenge Given a Divisor and a Bound , Find the largest integer N , Such That , Conditions : N is divisible by divisor N is less than or equal to bound N is greater than 0. Notes The parameters (divisor, bound) passed to the function are only positive values . It’s guaranteed that a divisor is Found .Input » Output Examples maxMultiple (2,7) ==> return (6) Explanation:...

November 29, 2022 · 2 min · 232 words · Andrew

How to Assign a Digital Cypher in C

The challenge Digital Cypher assigns to each letter of the alphabet unique number. For example: a b c d e f g h i j k l m 1 2 3 4 5 6 7 8 9 10 11 12 13 n o p q r s t u v w x y z 14 15 16 17 18 19 20 21 22 23 24 25 26 Instead of letters in encrypted word we write the corresponding number, eg....

November 28, 2022 · 3 min · 551 words · Andrew

How to Check for All Inclusive in C

The challenge Input: a string strng an array of strings arr Output of function contain_all_rots(strng, arr) (or containAllRots or contain-all-rots): a boolean true if all rotations of strng are included in arr false otherwise Examples: contain_all_rots( "bsjq", ["bsjq", "qbsj", "sjqb", "twZNsslC", "jqbs"]) -> true contain_all_rots( "Ajylvpy", ["Ajylvpy", "ylvpyAj", "jylvpyA", "lvpyAjy", "pyAjylv", "vpyAjyl", "ipywee"]) -> false) Note: Though not correct in a mathematical sense we will consider that there are no rotations of strng == "" and for any array arr: contain_all_rots("", arr) --> true The solution in C Option 1:...

November 27, 2022 · 3 min · 448 words · Andrew

How to Solve for Factorial in C

The challenge In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1. Write a function to calculate factorial for a given input. If input is below 0 or above 12 return -1 (C)....

November 26, 2022 · 1 min · 205 words · Andrew

How to Create an Incrementer in C

The challenge Given an input of an array of digits, return the array with each digit incremented by its position in the array: the first digit will be incremented by 1, the second digit by 2, etc. Make sure to start counting your positions from 1 ( and not 0 ). Your result can only contain single digit numbers, so if adding a digit with its position gives you a multiple-digit number, only the last digit of the number should be returned....

November 25, 2022 · 3 min · 613 words · Andrew

How to Return the Closest Number Multiple of 10 in C

The challenge Given a number return the closest number to it that is divisible by 10. Example input: 22 25 37 Expected output: 20 30 40 The solution in C Option 1: #include <math.h> int round_to_10 (int n) { return round(n / 10.0) * 10; } Option 2: int round_to_10(int n) { return (n + 5) / 10 * 10; } Option 3: int round_to_10 (int n) { int r = n % 10; if (r > 0) return r < 5 ?...

November 24, 2022 · 1 min · 176 words · Andrew

How to Reverse Every Other Word in a String in C

The challenge Reverse every other word in a given string, then return the string. Throw away any leading or trailing whitespace, while ensuring there is exactly one space between each word. Punctuation marks should be treated as if they are a part of the word in this challenge. The solution in C Option 1: #include <stddef.h> #include <stdbool.h> void reverse_alternate(const char *string, char *result) { bool is_word = false, is_second = false, not_first = false; for (const char *s = string; *s; ++s) { if (*s == ' ' && is_word) { is_word = false; is_second = !...

November 23, 2022 · 3 min · 533 words · Andrew

How to Solve Simple Beads Count in C

The challenge Two red beads are placed between every two blue beads. There are N blue beads. After looking at the arrangement below work out the number of red beads. @ @@ @ @@ @ @@ @ @@ @ @@ @ Implement count_red_beads(n) (countRedBeads(n)) so that it returns the number of red beads. If there are less than 2 blue beads return 0. The solution in C Option 1: int countRedBeads(n) { if(n<=1) return 0; return 2*(n-1); } Option 2:...

November 22, 2022 · 1 min · 135 words · Andrew

How to Solve the Maze Runner in C

The challenge Introduction Welcome Adventurer. Your aim is to navigate the maze and reach the finish point without touching any walls. Doing so will kill you instantly! Task You will be given a 2D array of the maze and an array of directions. Your task is to follow the directions given. If you reach the end point before all your moves have gone, you should return Finish. If you hit any walls or go outside the maze border, you should return Dead....

November 21, 2022 · 4 min · 821 words · Andrew