Advertisement
sylviapsh

Reverse Words In A Sentence Keep Punctuation

Jan 30th, 2013
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class ReverseWordsInASentencesKeepPunctuation
  5. {
  6.   //Write a program that reverses the words in given sentence.
  7.   //Example: "C# is not C++, not PHP and not Delphi!" -> "Delphi not and PHP, not C++ not is C#!".
  8.  
  9.  
  10.   static void Main()
  11.   {
  12.     string text = "C# is not C++, not PHP and not Delphi!"; //Text to reverse
  13.  
  14.     List<string> reversedSentence = new List<string>(); //List that stores the words of the sentence
  15.     List<string> punctuation = new List<string>();//List that stores the punctuation of the sentence
  16.  
  17.     string word = ""; //temporary string for each word construction
  18.     string separator = "";//temporary string for each punctuation construction (e.g. if you have comma and interval theay should be stored together as ", ")
  19.  
  20.     foreach (char item in text) //Iterate through the text
  21.     {
  22.       if (char.IsSymbol(item) || char.IsLetterOrDigit(item) || item == '#') //If you have a letter,digit or symbol
  23.       {
  24.         if (separator != "") //if the separator has value
  25.         {
  26.           punctuation.Add(separator);//add it to the punctuation list
  27.         }
  28.  
  29.         word += item; //construct a word
  30.         separator = ""; //Empty the separator constructor
  31.       }
  32.       else if (char.IsSeparator(item) || char.IsPunctuation(item) || char.IsWhiteSpace(item))//If you have a separator or punctuation
  33.       {
  34.         if (word != "")//if our word has value
  35.         {
  36.           reversedSentence.Add(word);//Add it to the list of words in the sentence that will be reversed
  37.         }
  38.  
  39.         separator += item;//construct a separator
  40.         word = "";//Empty the word constructor
  41.       }
  42.     }
  43.  
  44.     if (separator !="")//If we have a separator value left
  45.     {
  46.       punctuation.Add(separator);//add it to the punctuation list
  47.     }
  48.     if (word != "")//If we have a word value left
  49.     {
  50.       reversedSentence.Add(word);//Add it to the list of words
  51.     }
  52.  
  53.     //Print the output
  54.     int printLength = Math.Max(reversedSentence.Count, punctuation.Count); //Get the list with more elements
  55.     for (int k = 0, i = reversedSentence.Count - 1, j = 0; k < printLength; k++) //Cycle to the length of the longer list (k)
  56.     {
  57.       if (i >= 0)//Check for end of the words list
  58.       {
  59.         Console.Write(reversedSentence[i]);
  60.         i--;
  61.       }
  62.      
  63.       if (j < punctuation.Count)//Check for end of the punctuation list
  64.       {
  65.         Console.Write(punctuation[j]);
  66.         j++;
  67.       }
  68.     }
  69.     Console.WriteLine();
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement