Thursday 16 February 2017

Toggle String Lower to Upper and Upper to Lower

You have been given a String S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output.

using System; 
using System.Numerics;
public static class InvertStringExtension
{
    public static string Invert(this string s)
    {
        char[] chars = s.ToCharArray();
        for (int i = 0; i < chars.Length; i++)
            chars[i] = chars[i].Invert();
            
        return new string(chars);
    }
    public static char Invert(this char c)
    {
        if (!char.IsLetter(c))
            return c;
 
        return char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c);
    }
}
class MyClass {
    static void Main(string[] args) {
        var line1 = System.Console.ReadLine().Trim();
        System.Console.WriteLine(line1.Invert());
    }
}

No comments:

Post a Comment