JulianJulianov

01.TextProcessingLab-Reverse Strings

Apr 13th, 2020
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. 01. Reverse Strings
  2. You will be given series of strings until you receive an "end" command. Write a program that reverses strings and prints each pair on separate line in format "{word} = {reversed word}".
  3. Examples
  4. Input                      Output
  5. helLo                      helLo = oLleh
  6. Softuni                    Softuni = inutfoS
  7. bottle                     bottle = elttob
  8. end
  9.  
  10.  
  11. Dog                        Dog = goD
  12. caT                        caT = Tac
  13. chAir                      chAir = riAhc
  14. end
  15.  
  16.  
  17. using System;
  18. using System.Collections.Generic;
  19.  
  20. namespace TextProcessing_Lab
  21. {
  22.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             var word = string.Empty;
  27.             while ((word = Console.ReadLine()) != "end")
  28.             {
  29.                 var reversedWord = "";
  30.                 //for (int i = word.Length - 1; i >= 0; i--)            //Simple way.
  31.                 //{
  32.                 //    reversedWord += word[i];
  33.                 //}
  34.                 var listChar = new List<char>();                        //Other way.
  35.                 foreach (var itemChar in word)
  36.                 {
  37.                     listChar.Add(itemChar);
  38.                 }
  39.                 listChar.Reverse();
  40.                 foreach (var item in listChar)
  41.                 {
  42.                     reversedWord += item;
  43.                 }
  44.                 /*foreach (var item in word)                             //Genius way.
  45.                 {
  46.                     reversedWord += word[word.Length - word.IndexOf(item) - 1];
  47.                 }*/
  48.                 Console.WriteLine($"{word} = {reversedWord}");
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment