This function will revert string "how are you?" to "you are how?".
Please input 'STOP' to stop the console!
1
using
System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace HowAreYou
7 {
8 class Program
9 {
10 static void Main( string [] args)
11 {
12 string msg =
13 @" ********************************************
14 {0}
15 ******************************************** " ;
16 string usingMsg =
17 " Revert program.\r\n This function will revert string \ " how are you ? \ " to \ " you are how ? \ " .\r\n Please input 'STOP' to stop the console! " ;
18 Console.WriteLine(msg, usingMsg);
19 string stopMark = " STOP " ;
20
21 string input = string .Empty;
22 do
23 {
24 input = Console.ReadLine();
25 Console.WriteLine(Revert(input));
26
27 } while (input != stopMark); // "\r\n".
28
29 }
30 // how are you?
31 // you are how?
32 static string Revert( string target)
33 {
34 if (target == null || target == string .Empty)
35 {
36 return " Error:Can not revert empty string! Please enter input like this:How are you? " ;
37 }
38
39 // clear blank both end!
40 char blank = ' ' ;
41 if (target.StartsWith(blank.ToString()))
42 target = target.Trim();
43
44 StringBuilder builder = new StringBuilder();
45
46 int i = target.Length - 1 ;
47 int j = target.Length - 1 ;
48
49 if ( char .IsPunctuation(target[target.Length - 1 ]))
50 {
51 i = target.Length - 2 ;
52 j = target.Length - 2 ;
53 }
54
55 while (j > 0 )
56 {
57 if (target[j] == blank)
58 {
59 builder.Append(target.Substring(j + 1 , i - j));
60 builder.Append(blank.ToString());
61 i = j - 1 ;
62 }
63 j -- ;
64 }
65
66 builder.Append(target.Substring(j, i + 1 ));
67
68 if ( char .IsPunctuation(target[target.Length - 1 ]))
69 builder.Append(target[target.Length - 1 ].ToString());
70
71 return builder.ToString();
72 }
73
74 }
75
76 }
77
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace HowAreYou
7 {
8 class Program
9 {
10 static void Main( string [] args)
11 {
12 string msg =
13 @" ********************************************
14 {0}
15 ******************************************** " ;
16 string usingMsg =
17 " Revert program.\r\n This function will revert string \ " how are you ? \ " to \ " you are how ? \ " .\r\n Please input 'STOP' to stop the console! " ;
18 Console.WriteLine(msg, usingMsg);
19 string stopMark = " STOP " ;
20
21 string input = string .Empty;
22 do
23 {
24 input = Console.ReadLine();
25 Console.WriteLine(Revert(input));
26
27 } while (input != stopMark); // "\r\n".
28
29 }
30 // how are you?
31 // you are how?
32 static string Revert( string target)
33 {
34 if (target == null || target == string .Empty)
35 {
36 return " Error:Can not revert empty string! Please enter input like this:How are you? " ;
37 }
38
39 // clear blank both end!
40 char blank = ' ' ;
41 if (target.StartsWith(blank.ToString()))
42 target = target.Trim();
43
44 StringBuilder builder = new StringBuilder();
45
46 int i = target.Length - 1 ;
47 int j = target.Length - 1 ;
48
49 if ( char .IsPunctuation(target[target.Length - 1 ]))
50 {
51 i = target.Length - 2 ;
52 j = target.Length - 2 ;
53 }
54
55 while (j > 0 )
56 {
57 if (target[j] == blank)
58 {
59 builder.Append(target.Substring(j + 1 , i - j));
60 builder.Append(blank.ToString());
61 i = j - 1 ;
62 }
63 j -- ;
64 }
65
66 builder.Append(target.Substring(j, i + 1 ));
67
68 if ( char .IsPunctuation(target[target.Length - 1 ]))
69 builder.Append(target[target.Length - 1 ].ToString());
70
71 return builder.ToString();
72 }
73
74 }
75
76 }
77