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

mac2024-03-12  30

老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())); }
最新回复(0)