Program to Toggle Case of Each Character of String in C Language

Program-to-Toggle-Case-of-Each-Character-of-String-in-C-Language

Program Description:

Write a Program to Toggle Case of Each character of String in C programming language. The program should accept a string from the user and Invert the case or Toggle the case of all characters in the string.

Excepted Program Input and Output:

Input:

Enter a string : May the Force be With YOU

Output:

String after toggling each character: mAY THE fORCE BE wITH you

If you observe the above input and output of the program, The case of all characters in the input string is inverted or toggled.

Prerequisites:

It is recommended to go through the following articles to learn more about the Strings.

Algorithm to Toggle Case of Each Character of String in C:

We can toggle the character case by modifying the ASCII values of the string.

As we already discussed earlier ( Lowercase to Uppercase conversion, Uppercase to lowercase character conversion programs), characters in C language are stored in the memory with their ASCII Values.

For example, The character upper case A‘s ASCII Value is 65. Similarly lower case a‘s ASCII Value is 97.

You can check the list of ASCII Values by looking at the following post

To convert any Lowercase character to Upper Case character, We can subtract the value 32 from the ASCII number of the lowercase character.

For example,

  • Lowercase ‘d’ – ASCII Value – 100.
  • To convert to Upper case character – Subtract 32 from the lower case character. i.e 100 – 32 = 68
  • The ASCII value of the Upper case ‘D’ is 68.

Similarly, To convert any Uppercase character to a Lowercase character, We can Add the number 32 to the ASCII number of the uppercase character.

For example,

  • Uppercase ‘E’ – ASCII Value – 69.
  • To convert Uppercase to a Lowercase character – Add 32 to the Uppercase character. i.e 69 + 32 = 101
  • The ASCII value 101 is equal to the Lowercase character ‘e’

Toggle Case of Each Character of String in C Program Explanation:

Let’s look at the step-by-step explanation of the program.

  1. Start the program by declaring a string and naming it as inputStrwith the size SIZEelements. ( The SIZE is a constant with 100 value)
  2. Prompt the user to input a string and Update the inputStr with user-provided string.
  3. Now, To Toggle to the case of all characters, We will go through the all characters of the string using a For Loop, and based on the case of the string we either Add or Subtract the 32 from the character ASCII Value.
  4. Create a For loop to iterate over the string – for( i = 0; inputStr[i]; i++)
    • At Each Iteration, Check if the inputStr[i] is a lowercase character use if(inputStr[i] >= ‘a’ && inputStr[i] <= ‘z’) condition.
      • If the inputStr[i] is lower case character, Then Toggle it to upper case character by subtracting 32 from the ASCII value – inputStr[i] = inputStr[i] - 32;
    • If the inputStr[i] is an Upper case character(if(inputStr[i] >= ‘A’ && inputStr[i] <= ‘Z’)), Then Add the value 32 to it to make it a Lower case character.
      • i.e inputStr[i] = inputStr[i] + 32;
  5. Once the above step 4 is completed, Then all the characters in the given string will be toggled.
  6. Print the resultant string on the console using printf function.

Program to Toggle Case of Each Character of String in C using Iterative Method:

Here is the program to toggle case of all characters in a string

Program Output:

Let’s compile and run the program using GCC compiler (Any of your favorite compilers)

Test 1:

toggle-case-of-each-character-of-string-program-output

Let’s try another example

In the above example, The input string is Learn Programming at SillyCodes.com and the program toggled the all characters and provided the lEARN pROGRAMMING AT sILLYcODES.COM as output.

Test 2: When the input string contains numbers

As we are only changing the alphabets ( using the inputStr[i] >= ‘a’ && inputStr[i] <= ‘z’ conditions) other characters won’t be modified.

Toggle Case of Each Character of String in C using a user-defined function:

Let’s move the characters case toggle logic to a function, So that we can reuse the code. Here are few benefits of using the functions

In the following program, We defined a user defined function named toggleString to toggle all characters in the given string.

Here is the prototype details of the toggleString function.

void toggleString(char * str)

The toggleString function takes one formal argument named str and toggles or Inverts the case of all characters in the given string str. This function also modifies the ASCII values of the characters to convert them to upper case to lower case and vice versa.

Program Output:

Compile and Run the program.

toggle-case-of-each-character-of-string-using-user-defined-function

As we can see from the above output, The program is properly converting the character case.

Related String Practice Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Toggle Case of All Characters in String […]

Leave a Reply