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
    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    【leetcode】153. 寻找旋转排序数组中的最小值
    vue下载网络图片
    前端开发项目细节
    如何在手机上预览本地h5页面
    react拖拽添加新组件
    js拖入并复制和拖动改变位置和改变大小
    dva model
    postMessage跨源通信
    react-router
    event.stopPropagation()和event.preventDefault(),return false的区别
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576207.html
Copyright © 2011-2022 走看看