• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一蓑烟雨
C/C++,Linux,语音技术
博客园    首页    新随笔    联系   管理    订阅  订阅
C#:从字符串反转看字符串处理

题目:接收用户输入的一个字符串,将其中的字符以与输入相反的顺序输出。

方法一: 不可直接输出字符串,需要遍历输出

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace StrTest
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 string user = Console.ReadLine();
13 char[] a = user.ToCharArray();
14 Array.Reverse(a);
15 foreach (char c in a)
16 {
17 Console.Write("{0}",c);
18 }
19 }
20 }
21 }

方法二:利用数组

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace StrTest
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 string user = Console.ReadLine();
13 char[] a = user.ToCharArray();
14 for (int i = user.Length - 1; i >= 0; i--)
15 {
16 Console.Write("{0}",a[i]);
17 }
18 }
19 }
20 }

方法三:比方法二遍历快点,遍历二分之一

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace StrTest1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 string arr = Console.ReadLine();
13 char[] a = arr.ToCharArray();
14 int l = a.Length;
15 for (int i = 0; i < l/ 2; i++)
16 {
17 char temp = a[i];
18 a[i] = a[l-1-i];
19 a[l-1-i] = temp;
20 }
21 foreach (char j in a)
22 {
23 Console.Write("{0}",j);
24 }
25 }
26 }
27 }




 

posted on 2011-12-01 09:42  lovemu  阅读(7394)  评论(7)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3