C#练习题答案: 老switcheroo【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

老switcheroo【难度:1级】:

答案1:

using System.Text.RegularExpressions;

public class Kata
{
  public static string Vowel2Index(string str)
  {
   return  Regex.Replace(str, "[aeiou]", x => (x.Index + 1).ToString());
  }
}

答案2:

using System;
using System.Linq;
public class Kata {
  public static string Vowel2Index(string str) {
    return String.Concat(str.Select((c, i) => "aeiouAEIOU".IndexOf(c) != -1 ? "" + (i + 1) : "" + c));
  }
}

答案3:

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

public class Kata
{
        public static string Vowel2Index(string str)
        {
            char[] vowels = { 'a', 'e', 'o', 'i', 'u' };
            string[] strArray = new string[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                strArray[i] = str[i].ToString();
                if (vowels.Any(c => c == Convert.ToChar(strArray[i])))
                    strArray[i] = (i + 1).ToString();
            }
            return string.Concat(strArray);
        }
}

答案4:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
  private static readonly HashSet<char> Vowels = new HashSet<char> {
    'a', 'e', 'i', 'o', 'u'
  };

  public static string Vowel2Index(string str)
  {
    return string.Join(string.Empty, str.Select(ProcessCharacter));
  }
  
  private static string ProcessCharacter(char currentChar, int index) {
    return Vowels.Contains(currentChar) ? (index + 1).ToString() : currentChar.ToString();
  }
}

答案5:

public class Kata
{
  public static string Vowel2Index(string str)
  {
    string result = "";
    for (int i = 0; i < str.Length; i++)
    {
      if ( str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u'  )
        result += i+1;
      else
        result += str[i].ToString();
    }
    return result;
  }
}

答案6:

using System.Text.RegularExpressions;

public class Kata
{
  public static string Vowel2Index(string str)
  {
    return new Regex(".").Replace(str, Match => "aeiou".IndexOf(Match.Value.ToLower()) > -1 ? (Match.Index + 1).ToString() : Match.Value);
  }
}

答案7:

using System.Text.RegularExpressions;
using System.Linq;
public class Kata
{
    public static string Vowel2Index(string str) => string.Concat(str.Select((x,i)=>"aeiouAEIOU".IndexOf(x)<0?x.ToString():(i+1).ToString()));
}

答案8:

using System;
using System.Linq;
using System.Text.RegularExpressions;

public class Kata
{
  public static string Vowel2Index(string str)
  {
    return String.Concat(str.Select((c,i) => "aeiouAEIOU".Contains(c) ? (i+1).ToString() : c.ToString()));
  }
}

答案9:

using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
  public static string Vowel2Index(string str)
  {
    var vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' };
    var i = 0;
    
    return string.Join("", str.Select(c => { i++; return vowels.Contains(c) ? i.ToString() : c.ToString(); }));
  }
}

答案10:

using System.Linq;

public class Kata
{
    public static string Vowel2Index(string str) =>
        string.Concat(str.Select((e,i)=>"aeiou".Contains(e) ? (i+1).ToString() : e.ToString()));
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值