C#练习题答案: 停止gninnipS我sdroW!【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

停止gninnipS我sdroW!【难度:2级】:

答案1:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
    return String.Join(" ", sentence.Split(' ').Select(str => str.Length >= 5 ? new string(str.Reverse().ToArray()) : str));
  }
}​

答案2:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
      var words = sentence.Split(' ').Where(w => w.Length > 4);
            foreach (var w in words) sentence = sentence.Replace(w, Reverse(w));
            return sentence;
        }

        public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
}​

答案3:

using System.Linq;

public class Kata
{
    public static string SpinWords(string sentence)
    {
        string[] words = sentence.Split(' ');
        var reversedWords = words.Select(word => word.Length >= 5 ? string.Concat(word.Reverse()) : word);
        return string.Join(" ", reversedWords);
    }
}​

答案4:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
            string[] words = sentence.Split(' ');
            for (var i = 0; i < words.Count(); i++)
            {
                if (words[i].Length >= 5)
                {
                    words[i] = new string(words[i].Reverse().ToArray());
                }
            }

            return string.Join(" ", words);
    
  }
}​

答案5:

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

public static class StringJoinExtension
{
  public static string StringJoin(this IEnumerable<string> stringList, string seperator = "") => string.Join(seperator, stringList);
    
  public static string StringJoin(this IEnumerable<char> stringList, string seperator = "") => string.Join(seperator, stringList);  
}

public class Kata
{
  public static string SpinWords(string sentence) => 
      sentence
         .Split(' ')
         .Select(word => word.Length < 5 ? word : word.Reverse().StringJoin())
         .StringJoin(" ");              
}​

答案6:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
    string[] words = sentence.Split();

    for (int i = 0; i < words.Length; i++)
    {
        if (words[i].Length >= 5)
        {
            words[i] = new string(words[i].Reverse().ToArray());
        }
    }

    return string.Join(" ", words);
  }
}​

答案7:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
    string[] words = sentence.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            if (words[i].Length>=5)
            {
                words[i] = new string(words[i].Reverse().ToArray());
            }
        }

        return string.Join(" ", words);
  }
}​

答案8:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
   string[] words = sentence.Split();
            for (int i = 0; i < words.Length; i++)
            {
                if(words[i].Length >= 5)
                    words[i] = new string(words[i].Reverse().ToArray());
            }
            
            return string.Join(" ", words);
  }
}​

答案9:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
    string[] words=sentence.Split(" ");
    for(int i=0;i<words.Length;i++){
      if(words[i].Length>=5){
        words[i]= new string(words[i].Reverse().ToArray());
      }
    }
    return string.Join(" ",words);
  }
}​

答案10:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
    sentence.Split(' ').ToList().ForEach(word => { if(word.ToCharArray().Length >= 5) sentence = sentence.Replace(word,new String(word.Reverse().ToArray())); });
    return sentence;
  }
}​

答案11:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
    var words = sentence.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length > 4)
                {
                    var charArray = words[i].ToCharArray();
                    Array.Reverse(charArray);
                    words[i] = new string(charArray);
                }
            }
      return      string.Join(" ", words);
  }
}​

答案12:

using System.Linq;

public class Kata
{
  public static string SpinWords(string sentence)
  {
    return string.Join(" ", sentence.Split(' ').Select(s => s.Length > 4 ? string.Concat(s.Reverse()) : s));
  }
}​

答案13:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
      return string.Join(" ", sentence.Split(' ').Select(s => s.Length > 4 ? string.Concat(s.Reverse()) : s));
  }
}​

答案14:

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

public class Kata
{
  public static string SpinWords(string sentence)
  {
  string[] words = sentence.Split(' ');

  for(int i=0;i<words.Length;i++)
  { 
    if(words[i].Length >= 5)
    {
      char[] charArray = words[i].ToCharArray();
      Array.Reverse(charArray);
      words[i] = new string(charArray);
    }
  }
    string newSentence = string.Join(" ",words);
    return newSentence;
    
  }
}​




上一篇:python中pop()函数的用法


下一篇:【Python第25课】字符串的分割与拼接