首页 > 移动平台 > 详细

Case swapping

时间:2015-07-09 09:34:55      阅读:267      评论:0      收藏:0      [点我收藏+]

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)));
  }
}

 

Case swapping

原文:http://www.cnblogs.com/chucklu/p/4632118.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!