Obtain the first or last word inside a string in C++

If you do a lot of work with strings in C++, one thing you’ll probably find yourself doing is writing utility functions. The String class provides many useful built-in methods, but when it comes to searching, replacing and text manipulation, you may find that you’ll need to create your own functions to carry out these tasks, sometimes using and combining the built-in class methods to carry out more complex tasks within your projects.

The two following functions can be used to obtain the first and last words contained within a string. An empty string is returned if the function fails. The functions make use of the built in String methods find_first_of and find_last_of;

Having a decent set of your own utility functions can save a lot of time, and will make your code a lot more manageable. A good strategy for code re-usability is to break up a bigger task into smaller functions that you can see yourself re-using in the future, saving yourself both time and making your code easier to understand.

This is akin to your toolbox, and if you find yourself needing the same tool over and over again, then that’s when it’s a good idea to write your own utility function or use someone else’s. There’s always that temptation to write everything for yourself, but one of the key aspects of code re-usability is to not re-invent the wheel.

Writing your own utility functions does have some benefits, however. Firstly, you’ll understand the code very well and learn new techniques, and secondly, it can improve your overall skills as a programmer, especially in problem solving. So yes, there are times when it is better to write your own, and other times when using someone else’s solution is the best approach. You may even decide to compromise by checking other people’s solutions to help you write you own. There’s no right or wrong way to approach it. Take the approach that suits you or the situation best.

Prime numbers, a simple function in C++

A prime number is simply a number that is only divisible by one and itself. One of the most basic ways to find out if a number is prime, then, is to loop through every number (except for 1, as all positive integers are divisible by 1), and return true if the number is equally divisible by any other number except for itself.

This is a good example use of the modulus operator. The modulus operation returns the remainder of a division of two numbers. So, for example, 15 % 5 would equal 0, as 5 goes into 15 exactly 3 times with no remainder. 17 % 5 on the other hand would equal 2. As 5 goes into 17 3 times, with 2 being left over.

How it works:

Knowing this, and knowing what makes a prime number, we can check if a number is prime by checking every number between two and itself minus one, and if any of these numbers give us a modulus result of 0, then it is exactly divisible by another number and therefore not prime. If no modulus results of 0 are found by the time i is equal to x-1, then we know x to be prime. As each integer would always be divisible by itself, we have no need to check whether this is true or not. If we did want to check this, then our loop conditional statement would be i <= x;

This type of function is known as a predicate. A predicate is simply a function that returns true or false. In this instance, given an input of x, it will return true if the number is prime, and false if it is not.

If you wanted to implement this function and check every number beyond 1 to see if it was prime, you could do it like so;

This will iterate indefinitely until the maximum value that is able to be held in unsigned long int on your machine is reached. At that point, it will wrap around back to 0. You can use a larger data type, such as long long int, if your compiler supports it. But as you'll see, the main drawback of this function is that the larger the number gets, the more numbers it needs to check itself against to determine whether or not it is prime, so you may never reach that limit as it will take progressively longer each time to check the next number in the infinite while loop. The speed at which the program runs will depend on the processing power of the machine you are running it on.

If you needed to quickly check if a number is prime without needing to calculate it, then you could store the results in a text file, and check to see if that number exists in the file. If you store the results numerically, then you can stop searching once you reach the next prime number in the file beyond the one you're searching for. This is a very simple example of a lookup table, which saves computation time by storing the results of an operation rather than having to re-calculate them each time. This makes sense when you're performing a large number of checks of a particular calculation where the result is always going to be the same, and especially if you're going to be performing the same calculations many thousands of times over, and performance is a factor.

There are much more efficient techniques out there for finding prime numbers too, known as prime number sieves. One such algorithm is known as the Sieve of Eratosthenes, and I may follow up this post in future with a discussion on that.

Permute all possible string combinations in C++

This function takes in a string, and will permute every possible combination of arrangements of the characters in the string. Because the function simply iterates through every character, it can output duplicate results. In the case of the word “sells”, for example, the first and last s will be swapped, resulting in the same word being output. One lazy way around this would be to store each combination as a key -> value pair using the STL container class map, which would discard any duplicate keys (although it may be more accurate to say that it would replace the value stored at that key, which would be the duplicate word). Another way would be to use the STL container class vector with std::find, however with larger words this may be a less performance-friendly approach than using a map.

I wrote this function in response to this programming challenge. Using the given input of “cat”, this function actually outputs:


So, it doesn’t replicate the example output given by the challenge exactly, (same results, different order) however, the requirements were to find all permutations, and repeated outputs in the case of duplicate characters are allowed too, so the function satisfies the challenge requirements.

How it works:

The function contains two loops, with one nested within the other. The outer loop iterates through a single character at a time, executes the inner loop, and then swaps each character with the next character at index i+1. The inner loop takes each character from the back of the string, moves it to the front, and then prints the string to the console. Combining these two processes allows us to permute every possible combination of characters, and the function will work on any size string.

You’ll notice that the outer loop iterates while i is less than str.length()-1, rather than str.length(). Why? Because we’re swapping the character at position i of the string with the character at position i+1 at the bottom of the outer loop, we have to ensure we do not go out of bounds of the string. If we used str.length() instead, then on the last iteration of the outer loop when we assign str[i] = str[i+1];, we’d be attempting to access an index just beyond the length of the string, which would give us undefined behaviour. It could crash the program, access some other data stored in memory, or do nothing at all. In all likelihood it would eventually crash the program.

The outer loop will run while i is less than str.length()-1, so it will stop executing once i is equal to the last index location of the string, preventing us from attempting to access an element beyond the bounds of the string.

We also use str.length()-1 to ensure we assign the last character in the string to buf within the inner loop. The length method of the string class gives the total number of characters in the string, but as the index of a string starts at 0, the last index in the string is equal to str.length()-1

If you’re still not entirely sure how the function works, then try it for yourself. Try commenting out the inner loop entirely and running the code, or see what happens when you don’t swap the character at position i in the string with i+1 in the outer loop.