C#程序分别反转一个句子的每个单词? [英] C# program to reverse each word of a sentence separately ?

查看:90
本文介绍了C#程序分别反转一个句子的每个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是C#内置函数的代码,如何在没有内置函数的情况下执行此操作?



here's the code with C# built in functions, how do i do it without built in functions?

string str = Console.ReadLine();
           string strrev = "";
           char[] punctuation = new char[] { ' ', ',', '.','?', '!'};
           char[] inputAsChars = str.ToCharArray();

           string temp = "";
           foreach (char c in inputAsChars)
           {
               if (punctuation.Contains(c))
               {
                   char[] tx = temp.ToCharArray();
                   Array.Reverse(tx);
                   temp = new string(tx);
                   strrev += temp + c;
                   temp = "";
               }
               else
                   temp += c;
           }
           Console.WriteLine(strrev);
           Console.ReadLine();

推荐答案

string input = Console.ReadLine();
var query = Regex.Matches(input, @"\W+|(\w+)")
                 .Cast<Match>()
                 .Select(m=>m.Groups[1].Success
                            ? new string(m.Groups[1].Value.Reverse().ToArray())
                            : m.Value);
Console.WriteLine(string.Join(string.Empty, query));



干杯

Andi


Cheers
Andi


这篇关于C#程序分别反转一个句子的每个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆