Reverse words in a string without using string functions in C#


  1. static void Main(string[] args)
  2.         {
  3.             string inp = Console.ReadLine();
  4.             StringBuilder strb = new StringBuilder();
  5.             List<char> word = new List<char>();
  6.             for (int c = 0; c < inp.Length; c++)
  7.             {
  8.  
  9.                 if (char.IsWhiteSpace(inp[c]) == true || c == inp.Length)
  10.                 {
  11.                     string tsb = string.Empty;
  12.                     foreach (var item in word)
  13.                     {
  14.                         tsb += item;
  15.                     }
  16.                     strb.Insert(0, tsb);
  17.                     strb.Insert(0, ' ');
  18.                     word = new List<char>();
  19.                 }
  20.                 else
  21.                     word.Add(inp[c]);
  22.             }
  23.             string tsb1 = string.Empty;
  24.             foreach (var item in word)
  25.             {
  26.                 tsb1 += item;
  27.             }
  28.             strb.Insert(0, tsb1);
  29.             string output = strb.ToString();
  30.             Console.WriteLine(output);
  31.             Console.Read();
  32.         }

OUT PUT:

input string: This is nice blog

output string: blog nice is This

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s