Challenge 48

🚮

Remove Double Spaces

Open in Code Editor or Copy Instructions
...solutions at bottom of page...

Often when processing text, we encounter double spaces - this is common if concatenating when strings have leading/training spaces

To solve this, we often want to remove double spaces. Note: for real programming languages, most will support Regex, which can be used to achieve this

We will want to implement a function to remove double spaces from scratch though

Examples:

removeDoubleSpaces(" space is a very big place ") would return " space is a very big place "

Extension:

Generalise the function so we can choose what repeated characters we want to remove - another common example if having multiple sequential new line characters, like the string "line 1\n\n\n\nline 2" which will have 3 additional empty lines between lines 1 & 2 - your function should be able to take this string and return the result "line 1\nline 2". For this, it might be helpful to know the new line character is 10 in ASCII, though that's not required. Running OUTPUT ASC("\n") would display that to you

Solutions

IGCSE 0478 / O-Level 2210 A-Level 9618