simonradev

ReverseTheSentence

Dec 12th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ReverseSentenceAndNotPunctoation
  9. {
  10.     class ReverseSentence
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string sentense = Console.ReadLine();
  15.  
  16.             Regex words = new Regex(@"([a-zA-Z]+)");
  17.             Regex nonWords = new Regex(@"([^a-zA-Z]+)");
  18.  
  19.             MatchCollection allWords = words.Matches(sentense);
  20.             MatchCollection allNonWords = nonWords.Matches(sentense);
  21.  
  22.             List<string> singleWords = new List<string>();
  23.             List<string> singleNonWords = new List<string>();
  24.  
  25.             foreach (Match word in allWords)
  26.             {
  27.                 singleWords.Add(word.Groups[1].ToString());
  28.             }
  29.  
  30.             foreach (Match nonWord in allNonWords)
  31.             {
  32.                 singleNonWords.Add(nonWord.Groups[1].ToString());
  33.             }
  34.  
  35.  
  36.             StringBuilder result = new StringBuilder();
  37.             for (int i = singleWords.Count - 1, j = 0; i >= 0; i--, j++)
  38.             {
  39.                 result.Append(singleWords[i]);
  40.                 result.Append(singleNonWords[j]);
  41.             }
  42.  
  43.             Console.WriteLine(result);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment