🔠🔡

Toggle Case

Week 30

Leading on from challenge 22, we have another string formatting question - this time, you are required to implement a form of tOgGlE cAsE - that is where the characters alternate between upper/lowercase according to some rules

You often post comments online in this format with your eDgY hUmOuR and rather than having to manually hold/release the SHIFT key as you are typing, you want a program that can take an input string and return a toggle case string according to the following 5 parameters:

  1. input: the input string to toggle case
  2. startUpper: Boolean for whether the first character will be upper/lowercase
  3. consecutiveSameCase: the number of consecutive characters that will be the same case - this allows you to not only create the typical tOgGlEcAsE strings (value of 1), but also strings like toGGleCAse (consec value of 2 - e.g. 2 lower, then 2 upper, then 2 lower etc)
  4. ignoreNonLettersHA: Valid words characters are letters (a-z), hyphens (-) and apostrophes (') - this is since these punctuation symbols can be part of valid words (e.g. "can't" and "ice-cream"). This parameter specifies whether anything not a letter, hyphen or apostrophe should contribute to decrementing the count until next toggle - this parameter is used because if you have e.g. spaces/commas/periods, you might want these not to affect the case of the next valid letter. See the examples in the textarea below
  5. resetAfterSpace: Boolean for whether each new word after a space character should start with the same case as the original startUpper parameter

Below we will see some examples demonstrating the various parameters

We will see three more example quotes below this next textarea - all will use/output the following function calls:

Quote: ">I can't ea,t ice-cream! Sad..."

Quote: "a,aa-aaa,aa'a"

Quote: "In space, no one can hear you scream"

For your answer, you should use those exact same 8 function calls (in the same order) with each result on a new line as in the examples above - the string you should use is in the textbox below (i.e. including the quotation marks, part after the hyphen etc)

Hints

Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts

Release Day Hint
2 There are various ways to achieve this challenge, but you will most likely want a variable storing the number of characters until the case switches and the starting case - you can then loop over each character in the string
3 In the loop, you will want to determine if the character is a letter (ASCII ranges 65-90 for uppercase and 97-122 for lowercase - you could convert the input to one case, so you only have to check one range) and possibly have another variable storing whether it is an apostrophe or hyphen
4 If the character is a space and the resetAfterSpace parameter is true, you will want to set the case back to be the starting case, the number of characters until toggle to be the consecutiveSameCase value, append this character to the result and continue to the next loop iteration
5 If the character is a letter or it's not a letter AND the ignoreNonLettersHA is false, you will want to decrement the count until next swap - if this has reached 0, then you can toggle the case and reset the until next swap to be the consec parameter value
30 2026