Advertisement
Guest User

Untitled

a guest
Jan 19th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. //Write a program that reverses the words in given sentence.
  2. //  Example: "C# is not C++, not PHP and not Delphi!" -> "Delphi not and PHP, not C++ not is C#!".
  3.  
  4. using System;
  5.  
  6. class ReverseWordsInSentence
  7. {
  8.     static void Main()
  9.     {
  10.         string sSentence = "C# is not C++, not PHP and not Delphi!";
  11.         string[] asWordsAndSymbols = sSentence.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  12.         string[] asCleanWords = sSentence.Split(new char[] { ' ', ',', '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
  13.         Array.Reverse(asCleanWords);
  14.         string sTemp1 = null, sTemp2 = null;
  15.         for (int i = 0; i < asWordsAndSymbols.Length; i++)
  16.         {
  17.             if (string.Equals(asWordsAndSymbols[i], asCleanWords[asWordsAndSymbols.Length - i - 1]))
  18.             {
  19.                 Console.Write(asCleanWords[i] + " ");
  20.             }
  21.             else
  22.             {
  23.                 sTemp1 = asCleanWords[asWordsAndSymbols.Length - i - 1];
  24.                 sTemp2 = asWordsAndSymbols[i];
  25.                 Console.Write(asCleanWords[i] + sTemp2.Replace(sTemp1, "") + " ");
  26.             }
  27.         }
  28.         Console.WriteLine();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement