题目:接收用户输入的一个字符串,将其中的字符以与输入相反的顺序输出。
方法一: 不可直接输出字符串,需要遍历输出
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 }