Print A String Or Letter In Reverse Without Using The Predefined Function In C#

INTRODUCTION
  • We are all mostly using the predefined function for string reversal and we can't know how it works in the background to reverse a string or letter.
  • Now, I am going to explain how to print a string or letters in reverse without using the predefined function in the C# programming language.
  • If asked by the interviewer to write a program for reversing a string using the predefined function in .NET, a lot of candidates get confused because we mostly use the predefined functions only.
What is a Reverse String?
  • The string is a sequence of characters.
  • Reverse a string is used to change the position of characters of the string.
Example
  • Input String is: Corner
  • Reverse String is: renroC 
REQUIREMENTS
  • Visual Studio
  • Console Application 
STEP 1

Open Visual Studio and choose File->New->Project. Then, the "New Project" window will appear on the screen.


STEP 2

Next, to choose the Visual C# -> Console application, then give the name of your application in the New Project window, then browse the location for your project. After that, press the OK button to create your console application.


After that, write the code for string reversal.

Program for reversing a string using the predefined function
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace StringFunctions  
  8. {  
  9.     class Program  
  10.     {   
  11.       class stringFunction  
  12.       {  
  13.           public string StringReverse(string input)  
  14.           {  
  15.               char[] array = input.ToCharArray();  
  16.               Array.Reverse(array);  
  17.               return new string(array);  
  18.           }  
  19.   
  20.       }  
  21.         static void Main(string[] args)  
  22.       {  
  23.           stringFunction obj = new stringFunction();  
  24.           Console.WriteLine("Enter the string for String Reverse:");  
  25.           string strRev=Console.ReadLine();  
  26.           Console.WriteLine("Output:");  
  27.           Console.Write(obj.StringReverse(strRev));  
  28.           Console.ReadKey();  
  29.         }  
  30.     }  
  31. }  
Save and run the program to get the output.
OUTPUT

 

Program for reversing a string without using the pre-defined function
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication1  
  8. {  
  9.     class Program  
  10.     {  
  11.         public string reverse(string input)  
  12.         {  
  13.             string strreverse="";  
  14.             int len = input.Length-1;  
  15.             for (int i = len; i >= 0;i-- )  
  16.             {  
  17.                strreverse += input[i];  
  18.             }  
  19.   
  20.                 return (strreverse);  
  21.         }  
  22.         static void Main(string[] args)  
  23.         {  
  24.   
  25.             Program obj = new Program();  
  26.             Console.WriteLine("*****Without Using The Predefined Function******");  
  27.             Console.WriteLine("Enter the String to Reverse:");  
  28.             string strRev = Console.ReadLine();  
  29.             Console.WriteLine("output:");  
  30.             Console.Write(obj.reverse(strRev));  
  31.             Console.ReadKey();  
  32.               
  33.         }  
  34.     }  
  35. }  
Save and run the program to get the output.
Output

 
Conclusion

I hope that after reading this blog, you all know how to reverse a string or letter with and without a predefined function in C# programming language.