We need to write a simple C# code to demonstrate a reversing the given input string at runtime. Below is the code snippet to achive this.
class Program { internal static void ReverseString(string str) { char[] charArray = str.ToCharArray(); for (int i = 0, j = str.Length - 1; i < j; i++, j--) { charArray[i] = str[j]; charArray[j] = str[i]; } string reversedstring = new string(charArray); Console.WriteLine(reversedstring); } static void Main(string[] args) { ReverseString(args[0]); Console.ReadKey(); } }
class Program
{
internal static void ReverseString(string str)
char[] charArray = str.ToCharArray();
for (int i = 0, j = str.Length - 1; i < j; i++, j--)
charArray[i] = str[j];
charArray[j] = str[i];
}
string reversedstring = new string(charArray);
Console.WriteLine(reversedstring);
static void Main(string[] args)
ReverseString(args[0]);
Console.ReadKey();
In more simple way we can revese string like this.
static void ReverseString(string str) { string nStr = string.Empty; foreach (var item in str) { nStr = item + nStr; } Console.WriteLine(nStr); }
static void ReverseString(string str)
string nStr = string.Empty;
foreach (var item in str)
nStr = item + nStr;
Console.WriteLine(nStr);
Public static void Program() {string input =Console.ReadLine();string Reverse(string str) {string result = string.Empty;var x = str.ToCharArray();for(int i = x.Length - 1; i >=0 ;i--){result = x[i] ;} return result;} Console.WriteLine(Reverse(input)); }
public static string Reverse( string s ) {char[] charArray = s.ToCharArray();Array.Reverse( charArray );return new string( charArray ); }
public static string ReverseString(string input) { string output = ""; for (int i = input.Length-1; i >= 0; i--) { output+=input[i]; } return output; }
public static string ReverseString(string input)
string output = "";
for (int i = input.Length-1; i >= 0; i--)
output+=input[i];
return output;