## How to get the Length of an Array in C

There isn’t really a standard way to get the length of array in C. This means you need to do some additional work to achieve getting the length of an array when using C. Creating and Looping through an Array in C Usually you would create an array as follows: int items = {1, 2, 3, 4, 5}; You could always define the size right off the bat: const int SIZE = 5; int items[SIZE] = {1, 2, 3, 4, 5}; This way if you need to loop through the array later on, you can use your SIZE variable....

June 14, 2022 · 2 min · 228 words · Andrew

## How to Sum the Two Lowest Positive Integers in C++

The challenge Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed. For example, when an array is passed like [19, 5, 42, 2, 77], the output should be 7. [10, 343445353, 3453445, 3453545353453] should return 3453455. The solution in C++ Option 1: #include <algorithm> #include <vector> long sumTwoSmallestNumbers(std::vector<int> numbers) { std::sort(numbers....

June 7, 2022 · 2 min · 301 words · Andrew

## How to Find the Shortest Word in C++

The challenge Simple, given a string of words, return the length of the shortest word(s). The string will never be empty and you do not need to account for different data types. The solution in C++ Option 1: int find_short(const std::string &str) { std::istringstream inp(str); std::string s; int len = -1; while (std::getline(inp, s, ' ')) if (s.length() < len) len = s.length(); return len; } Option 2: int find_short(std::string str) { std::istringstream iss(str); std::vector<std::string> vec{ std::istream_iterator<std::string>(iss), {} }; return std::min_element(vec....

May 27, 2022 · 1 min · 193 words · Andrew

## How to Square Every Digit in C++

The challenge You are asked to square every digit of a number and concatenate them. For example, if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1. Note: The function accepts an integer and returns an integer The solution in C++ Option 1: int square_digits(int n) { int a = 1; int m = 0; while (n > 0) { int d = n % 10; m += a * d * d; a *= d <= 3 ?...

May 26, 2022 · 2 min · 302 words · Andrew

## How to Count Vowels in C++

The challenge Return the number (count) of vowels in the given string. We will consider a, e, i, o, u as vowels for this challenge (but not y). The input string will only consist of lower case letters and/or spaces. The solution in C++ Option 1: #include <string> using namespace std; bool is_vowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } int getCount(const string& inputStr) { return count_if(inputStr....

May 25, 2022 · 2 min · 415 words · Andrew