zoukankan      html  css  js  c++  java
  • 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
    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    将一张图片的人物融入另一张图片中
    填充---内容识别图片
    使用蒙版--渐变--制作瓶子倒影
    form表单基础知识
    表格排版及其表格嵌套
    HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列
    垃圾收集,内存问题
    JS预解析机制
    python ==》 内置函数
    python ==》 递归
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576207.html
Copyright © 2011-2022 走看看