您的位置:首页 > 其它

Find the capitals

2015-07-05 18:48 369 查看

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

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: