• C#将输入的一句话中的所有单词倒置


    问题来自百度知道:


    要求不要开辟另外的内存空间,我应该没做到。但至少效果实现了。

    我的方法是:

            static void Main(string[] args)
            {
                string sentence = "Recetly, hospitals in many";
                Console.WriteLine("  原来的句子:" + sentence);
                Console.WriteLine("处理后的句子:" + ReverseSentence(sentence));
                Console.Read();
            }
    
            static string ReverseSentence(string sentence) 
            {        
                char[] chars = sentence.ToCharArray();
                sentence = "";
                for (int i = chars.Length - 1; i >= 0; i--)
                {
                    sentence += chars[i];
                }
                //Console.WriteLine(sentence);
                Regex regex = new Regex(@"(\b\w+\b)|(\W+)");
                MatchCollection matches = regex.Matches(sentence);
                //Console.WriteLine(matches.Count);
                string str = "";
                for (int i = matches.Count - 1; i >= 0;i-- )
                {
                    str += matches[i].ToString();
                }
                return str;
            }

    运行结果:   

     

    那个逗号的位置不对,下面是修改后的:

    	static void Main(string[] args)
            {
                string sentence = "Recetly, hospitals in many";
                Console.WriteLine("  原来的句子:" + sentence);
                Console.WriteLine("处理后的句子:" + ReverseSentence(sentence));
                Console.Read();
            }
    
            static string ReverseSentence(string sentence)//反转句子
            {
                sentence = ReverseString(sentence.Trim());
                Regex regex = new Regex(@"(\b\w+\b)|(\W+)");
                MatchCollection matches = regex.Matches(sentence);
                string str = "";
                for (int i = matches.Count - 1; i >= 0; i--)
                {
                    if(i%2 == 0)
                    {
                        str += matches[i].ToString();
                    }
                    else
                    {
                        str += ReverseString(matches[i].ToString());
                    }
                }
                return str;
            }
    	static string ReverseString(string str)//反转单个字符串
            {
                char[] chars = str.ToCharArray();
                str = "";
                for (int i = chars.Length - 1; i >= 0; i--)
                {
                    str += chars[i];
                }
                return str; 
            }


    运行结果:

    本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/07/06/4576207.html

    本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/07/06/4576207.html
    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    UIAutomator简介
    初识Anrdiod SDK
    Andriod App类型简介
    Mybatis之collection嵌套查询mapper文件写法
    Ubuntu16.04 启动纯文本界面方法
    Ubuntu16.04网络不能访问解决办法
    JavaScript之简易http接口测试工具网页版
    SpringBoot之修改单个文件后立刻生效
    JavaScript之Json的使用
    Ajax之Jquery封装使用举例2(Json和JsonArray处理)
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576207.html
Copyright © 2020-2023  润新知