Revision task 1

First of all, before diving into the task solution, we should say that there isn’t any one definitive way to solve it. There are many ways, some better than others. Due to this, i’ll provide a couple of different solutions to this problem. First of all however, let’s take a look at a proposed main() function and the function prototype for the function we’ll create.

A couple of takeaways:

  • We don’t want to modify the original data so we need to create another variable that can hold the modified string.
  • The new variable must be the same length as the original, as there may be no vowels at all.
  • Don’t forget 1 for the terminator.
  • We could always find the length of the string in the subfunction but since we already needed it in the main function, it won’t hurt to just pass it.
  • As you can see, the variable names are different in main() and the proposed prototype. This is just to remind ourselves that they are in a different scope.

Now for the implementation of the subfunction. All of these have the general basic idea:

  • We need to go through each of the characters in the original string
  • We need to look for vowels
  • There are both lower and upper case characters
  • We only copy over anything that is not a vowel – including punctuation. This means we need another variable to keep track of how many we have copied over
  • The resulting string needs a terminator. We can either set it manually or zero-initialize the whole string.
The first solution will work and tick all of our check marks but it isn’t the prettiest thing out there. The condition is huge and doesn’t really look that good.
The second option looks a lot better but is basically still the same construct, however readability of this solution has improved.
The third option that we can use is probably the most complex in structure but it works well with even if we have a lot of cases that we need to check against. This time we are using a reference array to compare our values against and if no vowels were found, we can copy the character over.

Lastly, there is an optimization that we can do. Why check both upper and lower case characters when we could just convert it to lower case and check against half of them. This will save us a lot of CPU time if the problem gets larger. We can either do this by using a standard library function tolower() from ctype.h library or use binary conversion.

Now for the last thing, let’s look at the binary conversion.  We want to set the 7th bit in the character as 1. This is because the only difference between lowercase and uppercase characters is that one single bit.