Advertisement
butoff

Reverse_A_String

Oct 4th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace DictionaryTest
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             string str = "abc_def_ghj";
  11.             char delimiter = '_';
  12.             string temp = "";
  13.             string result = "";
  14.  
  15.             if (str.IndexOf(delimiter) == -1)
  16.             {
  17.                 return;
  18.             }
  19.  
  20.             for (int i = str.Length - 1; i >= 0; i--)
  21.             {
  22.                 if (str[i] != delimiter)
  23.                 {
  24.                     temp += str[i];
  25.                 }
  26.                 else
  27.                 {
  28.                     string span = new string(temp.Reverse().ToArray());
  29.                     span += delimiter;
  30.                     result += span;
  31.                     temp = "";
  32.                 }
  33.  
  34.                 // If loop is over but temp still contains characters
  35.                 if(i == 0 && temp.Length > 0)
  36.                 {
  37.                     string span = new string(temp.Reverse().ToArray());
  38.                     result += span;
  39.                 }
  40.             }
  41.  
  42.             Console.WriteLine(result);
  43.             Console.ReadLine();
  44.         }        
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement