加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在没有第一个的情况下反转数组中的所有单词

发布时间:2020-12-15 23:40:55 所属栏目:百科 来源:网络整理
导读:我试图让以下代码工作,所以如果 Input is: How are you dude? 输出应该是: How era uoy edud? 我认为我非常接近完成它但我无法理解为什么,正则表达式不起作用它不被识别. 这是repl:https://repl.it/MHzu/1 using System.Collections.Generic;using System.
我试图让以下代码工作,所以如果

Input is: How are you dude?

输出应该是:

How era uoy edud?

我认为我非常接近完成它但我无法理解为什么,正则表达式不起作用它不被识别.
这是repl:https://repl.it/MHzu/1

using System.Collections.Generic;
using System.Linq;
using System;
using System.Text.RegularExpressions;

public class Kata
{    
   static void Main(string[] args)
    {
          string str = Console.ReadLine();            
          string opaa = str;
          Match m = Regex.match(str,"(w*) (w.*)");
          string hoho = m.Groups[1];
          string strrev = "";
          foreach (var word in opaa.Split(' '))
          {
              string temp = " ";
              foreach (var ch in word.ToCharArray())
              {
                 temp = ch + temp;
              }
              strrev = strrev + temp + "";
          }
          Console.WriteLine(hohoo + strrev);  
    }
}

解决方法

你也可以使用Linq

string input = "think that I am very close to finish";

var  output = string.Join(" ",input.Split()
                                   .Select((x,i) => i == 0 ? x : string.Concat(x.Reverse())));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读