Find the capitals

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)