Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Write a program that reverses the words in given sentence.
- /// Example: "C# is not C++, not PHP and not Delphi!" "Delphi not and PHP, not C++ not is C#!".
- using System;
- using System.Text;
- class zad13
- {
- static void Main()
- {
- string text = "C# is , , not C++, not . . . PHP and not Delphi!";
- char[] separators = new char[] { '!', '.', ',', ' ', };
- string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- string[] wordsWith = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- Array.Reverse(words);
- int wordsWithIndex = 0;
- for (int i = 0; i < words.Length; i++)
- {
- // get last char
- string temp = wordsWith[wordsWithIndex];
- char symbol = temp[temp.Length - 1];
- if ((symbol == '!' || symbol == ',' || symbol == '.') && temp.Length > 1)
- {
- wordsWith[wordsWithIndex] = wordsWith[wordsWithIndex].Remove(0, wordsWith[wordsWithIndex].Length - 1);
- wordsWith[wordsWithIndex] = words[i] + wordsWith[wordsWithIndex];
- }
- else if (temp.Length > 1) // skip floating symbols
- {
- wordsWith[wordsWithIndex] = words[i];
- }
- else
- {
- i--;
- }
- wordsWithIndex++;
- }
- StringBuilder newStr = new StringBuilder();
- for (int i = 0; i < wordsWith.Length; i++)
- {
- newStr.Append(wordsWith[i] + " ");
- }
- Console.WriteLine(newStr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement