码迷,mamicode.com
首页 > 其他好文 > 详细

makeBackronym

时间:2015-06-30 21:49:37      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

makeBackronym  主要考查的是字符串的处理,大小写转换,以及字符串的Linq处理

Description:

Definition-

back·ro·nym

noun

a fanciful expansion of an existing acronym or word, such as “port out, starboard home” for posh.

You will create a function called makeBackronym . There will be a preloaded dictionary to use. The dictionary is an object where the the keys are letters A-Z and the values are a predetermined word.

Use the variable name (its name is written in the code template) to reference the uppercase letters of the dictionary.

EXAMPLE:

dict[‘P‘]=="perfect"

There will be a string(without spaces) passed into the function that you need to translate to a Backronym.

The preloaded dictionary can only read uppercase letters, and the value you return will have to be a string.

EXAMPLES:

"dgm" -> "disturbing gregarious mustache"

"lkj" -> "literal klingon joke"

using System;
public partial class Kata
{
  public static string MakeBackronym(string s)
  {
      s = s.ToUpper();
      string str = string.Empty;
      foreach(var item in s)
      {
          str += dict[item]+" ";
      }
      if(str.Equals(string.Empty)==false)
      {
         str = str.Substring(0,str.Length-1);
      }
      return str;
  }
}

 

其他解法:设计到string.Join以及string.ToUpper  以及Linq中的select

using System.Linq;

public partial class Kata
{
  public static string MakeBackronym(string s)
  {
    return string.Join(" ", s.ToUpper().Select(c => dict[c]));
  }
}

 

makeBackronym

标签:

原文地址:http://www.cnblogs.com/chucklu/p/4611501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!