Reversing The Words In Sentence Using ASP.NET C#

Way1: Using Array.Reverse() method

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ProgrammingInterviewFAQ.ReverseWordsInSentence {  
  7.     class UsingReverseFunction {  
  8.         static void Main() {  
  9.             string input = "karthik is a software engineer";  
  10.             string output = string.Empty;  
  11.             string[] wordsininput = input.Split(' ');  
  12.             Array.Reverse(wordsininput);  
  13.             output = string.Join(" ", wordsininput);  
  14.             Console.WriteLine(output);  
  15.             Console.ReadLine();  
  16.         }  
  17.     }  
  18. }  
Way2 : without using Array.Reverse() method

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace ProgrammingInterviewFAQ.ReverseWordsInSentence {  
  7.     class WithoutUsingReverseFuntion {  
  8.         static void Main() {  
  9.             string input = "karthik is a developer";  
  10.             string output = string.Empty;  
  11.             string[] wordsininput = input.Split(' ');  
  12.             int lengthofinput = wordsininput.Length;  
  13.             int Arrayindextoprocess = lengthofinput - 1;  
  14.             for (int i = 0; Arrayindextoprocess >= 0; i++) {  
  15.                 output = output + " " + wordsininput[Arrayindextoprocess];  
  16.                 Arrayindextoprocess--;  
  17.             }  
  18.             Console.WriteLine(output);  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }