A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 嘴角上揚ぃ读不 于 2014-4-23 17:11 编辑

接收用户输入的一句英文,将其中的单词以反序输出。例如:my love-ym-evol

5 个回复

倒序浏览
本帖最后由 鲤鱼 于 2014-4-23 12:15 编辑

帮顶好了{:3_68:}
回复 使用道具 举报
  1.             string str = "my love";//可改Console.ReadKey();
  2.             //分割字符串,以空格作为分隔符
  3.             string[] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  4.             for (int i = 0; i < strs.Length; i++)
  5.             {
  6.                 //遍历字符串数组
  7.                 string strNew = "";
  8.                 for (int j = strs[i].Length-1; j >=0 ; j--)
  9.                 {
  10.                     //实现字符串反序
  11.                     strNew += (strs[i][j]).ToString();
  12.                 }
  13.                 strs[i] = strNew;//将反序后的字符串赋值回给字符串数组
  14.             }
  15.             string res = String.Join(" ",strs);//用用空格拼接字符串
  16.             Console.WriteLine(res);
  17.             Console.ReadKey();
复制代码

按你要求写了下,这是代码和注释

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
回忆~坚 发表于 2014-4-23 12:45
按你要求写了下,这是代码和注释

知道了  ,  谢谢啦
回复 使用道具 举报
本帖最后由 伪善者。 于 2014-4-23 20:54 编辑

static void Main(string[] args)
        {
            string s = "love of my life";
            string[] str = s.Split();//将字符串按空格拆分成字符串类型的数组.

            for (int i = 0; i < str.Length; i++)//循环遍历字符串数组中各元素
            {
                char[] temps = str.ToCharArray();//把每一个字符串按倒序输出
                for (int j =temps.Length-1; j>=0; j--)
                {
                   Console.Write(temps[j]);
                }
                if (i == str.Length - 1)
                    break;
                Console.Write("-");//用"-"号连接
            }
            Console.ReadKey();
        }
没有最后生成字符串 直接输出了

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
伪善者。 发表于 2014-4-23 20:52
static void Main(string[] args)
        {
            string s = "love of my life";

     谢谢啦   
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马