4.4 Toggle the Case of Each Character in a String in C
4. Basic String Algorithm Problems in C
Full Course on Data Structures and Algorithms in C
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):
#includevoid toggleChars(char str[], int N) { // Conversion according to ASCII values for (int i = 0; i < N; i++) { if (str[i] >= 'a' && str[i] <= 'z') // Convert lowercase to uppercase str[i] = str[i] - 32; else if (str[i] >= 'A' && str[i] <= 'Z') // Convert uppercase to lowercase str[i] = str[i] + 32; } } int main(){ int N = 0; //length (size) of string to be determined in function below char stri[] = "Broad Network Corporation"; //given string char *ptr = stri; while (*ptr != '\0') { //in C a string ends with '\0', which should not be counted ptr++; N += 1; } toggleChars(stri, N); // Print toggled characters for(int i = 0; i < N; i++) printf("%c ", stri[i]); printf("\n"); return 0; }
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):
#include#include void toggleChars(char str[], int N) { // Iterate over the same string for (int i = 0; i < N; i++) { // Check the case of the character and // toggle accordingly if (isupper(str[i])) str[i] = tolower(str[i]); else str[i] = toupper(str[i]); } } int main(){ int N = 0; //length (size) of string to be determined in function below char stri[] = "Broad Network Corporation"; //given string char *ptr = stri; while (*ptr != '\0') { //in C a string ends with '\0', which should not be counted ptr++; N += 1; } toggleChars(stri, N); // Print toggled characters for(int i = 0; i < N; i++) printf("%c ", stri[i]); printf("\n"); return 0; }
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.