码迷,mamicode.com
首页 > Windows程序 > 详细

Find the capitals

时间:2015-07-05 19:41:45      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

Find the capitals

Description:

Instructions

Write a function that takes a single string (word) as argument. The function must return an ordered list containing the indexes of all capital letters in the string.

Example

Assert.AreEqual(Kata.Capitals("CodEWaRs"), new int[]{0,3,4,6});

 

using System;
using System.Linq;

public static class Kata
{
  public static int[] Capitals(string word)
  {
    //Write your code here
    int[] array = new int[] { };
            if (word == null || word == string.Empty)
            {
                return array;
            }
            string tmp = word.ToLower();
            return Enumerable.Range(0, tmp.Length).Where(i => word[i] != tmp[i]).ToArray();
  }
}

 

 

其他人的解法:

值得学习的是char本身自带了判断是否大写字母的函数

using System.Collections.Generic;
using System;

public static class Kata
{
  public static int[] Capitals(string word)
  {
     var capitalIndexes = new List<int>();

      for (var i = 0; i < word.Length; i++)
      {
        if (char.IsUpper(word[i]))
          capitalIndexes.Add(i);
      }

      return capitalIndexes.ToArray();
  }
}

 

 

 

 




Find the capitals

标签:

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

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