zoukankan      html  css  js  c++  java
  • Case swapping

    Case swapping

    Description:

    Given a string, swap the case for each of the letters.

    e.g. CodEwArs --> cODeWaRS

    Examples

    Kata.Swap("") == ""
    Kata.Swap("CodeWars") == "cODEwARS"
    Kata.Swap("abc") == "ABC"
    Kata.Swap("ABC") == "abc"
    Kata.Swap("123235") == "123235"
    using System;
    using System.Linq;
    
    public static class Kata 
    {
     public static string Swap(string str)
            {
                return string.Join(string.Empty, str.Select(character => char.IsLower(character) ? char.ToUpper(character) : char.IsUpper(character) ? char.ToLower(character) : character));
            }
    
            //public static string Swap(string str)
            //{
            //    str = string.Join(string.Empty, str.Select(Selector));
            //    return str; //your code here
            //}
    
            //public static char Selector(char character)
            //{
            //    char tempCharacter = character;
            //    if (char.IsLower(character))
            //    {
            //        tempCharacter = char.ToUpper(character);
            //    }
            //    else if (char.IsUpper(character))
            //    {
            //        tempCharacter = char.ToLower(character);
            //    }
            //    return tempCharacter;
            //}   
    }

    其他人的解法

    需要学习的是:char.ToUpper以及char.ToLower本身可以处理非大小写的字符,不需要另外多一个判断

    using System;
    using System.Linq;
    
    public static class Kata {
      public static string Swap(string str) {
        return String.Concat(str.Select(c => Char.IsUpper(c) ? Char.ToLower(c) : Char.ToUpper(c)));
      }
    }
  • 相关阅读:
    poj3264
    codevs4373 窗口==poj2823 Sliding Window
    BZOJ 3831
    1107 等价表达式
    codevs4600 [NOI2015]程序自动分析==洛谷P1955 程序自动分析
    BZOJ 1050
    1294 全排列[多种]
    BZOJ 2456
    BZOJ 3725
    BZOJ 3043
  • 原文地址:https://www.cnblogs.com/chucklu/p/4632118.html
Copyright © 2011-2022 走看看