Toggling String Without Inbuilt Functions - C#

Here is a small code snippet, which helps you toggle the string characters. 
  1. string x="This is a String";    
  2. string toggled= new string(x.Select(c => (char)(c > 64 && c < 91 ? c + 32 : (c>96 && c<123?c-32:c))).ToArray());    
  4. // You should see output string as below.    
  5. //tHIS IS A sTRING   

Basically, I just selected character array and for each character, it checks whether it is falling under the range 65-90 (ASCII range for a-z) or 97-122 (ASCII range for A-Z).

Using ternery operator, I am adding the value to the ASCII value of the character and at the end, I am converting IEnumerable to Array and passing it to the string constructor.

Please let me know, if you have question or comments.

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now