4.4 Toggle the Case of Each Character in a String in JavaScript
4. Basic String Algorithm Problems in JavaScript
Full Course on Data Structures and Algorithms in JavaScript
By: Chrysanthus Date Published: 3 Feb 2026
The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.
You are given a string. You are asked to toggle the case of each character: A) given that the string consists of ASCII characters, and B) the character set/code is not known.
You are asked to use O(N) time and O(N) space for each problem.
Example of Toggling Character Case
Input : s = "Broad Network Corporation" Output : "bROAD nETWORK cORPORATION"
A) The string consists of ASCII Characters
In the ASCII Code Table, if 32 is added to the code of an uppercase letter, the lowercase code is obtained; if 32 is subtracted from the code of a lowercase letter, the uppercase code is obtained. With that, just iterate over the given string. The program is (read through the code and comments):
"use strict";
function toggleChars(stri) {
let strArray = Array.from(stri);
let N = strArray.length;
for (let i = 0; i < N; i++) {
let code = strArray[i].charCodeAt(0);
if (code >= 97 && code <= 122) { // 'a' to 'z'
strArray[i] = String.fromCharCode(code - 32);
} else if (code >= 65 && code <= 90) { // 'A' to 'Z'
strArray[i] = String.fromCharCode(code + 32);
}
}
let str = strArray.join("");
console.log(str);
}
//main area
let stri = "Broad Network Corporation"; //given string
toggleChars(stri);
The output is:
b R O A D n E T W O R K c O R P O R A T I O N
as expected. The time complexity is actually O(2N), with O(N) for finding the length of the input string array and O(N) for iterating over the string array in the called function. However, the coefficient (multiplier of 2 or 3 or 1/2, etc.) is normally omitted. The space complexity is O(N), since the input string array and the string array in the called function are the same array.
B) The Character Set and its Coding is not known
When the character set and its corresponding coding are not known, import the ctype.h library which has the isupper(), tolower() and toupper() predefined functions. Each of these functions operate in approximately O(1) time, which is ignored when quoting the time complexity. Any temporary space used by these predefined functions are also ignored when quoting the space complexity. The following program illustrates the use of these functions to toggle the case of characters in a string (read through the code and comments):
"use strict";
function toggleChars(stri) {
let strArray = Array.from(stri);
let N = strArray.length;
// Iterate over the same string
for (let i = 0; i < N; i++) {
// Check the case of the character and
// toggle accordingly
if (strArray[i] === strArray[i].toUpperCase())
strArray[i] = strArray[i].toLowerCase();
else
strArray[i] = strArray[i].toUpperCase();
}
let str = strArray.join("");
console.log(str);
}
//main area
let stri = "Broad Network Corporation"; //given string
toggleChars(stri);
The output is:
b R O A D n E T W O R K c O R P O R A T I O N
as expected. The time complexity is officially O(N) and the space complexity is officially O(N)
Thanks for reading.
Related Links
More Related LinksCousins
BACK NEXTComments
Note: You can use the Search Box above to find articles and discussions of interest.