zoukankan      html  css  js  c++  java
  • 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();
      }
    }



  • 相关阅读:
    MySQL用户管理
    MySQL函数
    MySQL数据类型
    MySQL配置
    PowerDesigner之PDM检查
    PowerDesigner之PDM(物理概念模型)
    .NET Reflector反编译的方法
    IBatis.net 输出SQL语句(七)
    SVN 记录冲突、忽略
    SQLServer 窗口函数
  • 原文地址:https://www.cnblogs.com/chucklu/p/4622796.html
Copyright © 2011-2022 走看看