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(); } }
相关文章推荐
- 大数据环境下基于R语言的数据挖掘平台 之 环境配置
- Ardunio_Case_3_SOS
- Set 技巧之一
- 5.4-1
- JavaScript之XML总结
- java模板方法模式
- 实习前的准备工作:Android开发环境的搭建
- 编译原理第四章(1)
- 解决 linux 系统 centos 6.4 系统中 java -version和javac -version版本不一致
- 出现“此计算机上的windows防火墙阻止了远程调试”
- centos yum安装epel源
- 为ActiveX控件添加对话框
- epoll示例
- centos中安装DNS可能遇到的问题
- poj 2660 War on Weather 计算几何
- android 相对布局属性
- python httplib2应用get post
- 【java】单例模式涉及的多线程问题
- 谈谈你对流行框架的理解(
- CentOS下安装福昕PDF软件