Write a C program with a method public static void PrintReve

Write a C# program with a method, public static void PrintReverse(string[] array). Than print elements in array from right to left.

Example elements input: \"the\" \"soggy\" \"can\" \"was\"

Example element output would print: \"was\" \"can\" \"soggy\" \"the\"

Thanks for the help!

Solution

using System.IO;
using System;

class Program
{
static void Main()
{
Console.WriteLine(\"Enter the array size\");
int size;
size=Convert.ToInt32(Console.ReadLine());// taking the size of the array
string[] arr = new string[size];// declaring an array of size
Console.WriteLine(\"Enter the elements\");
// taking the array input
for(int i=0;i<size;i++){
arr[i]=Console.ReadLine();
}
Console.Write(\"Example elements input: \");
//printing the array before reverse
for(int i=0;i<size;i++){
Console.Write(\"{0} \", arr[i]);
  
}
Console.WriteLine();
//calling the reverse method. the method will reverse the existing array
PrintReverse(arr);
Console.Write(\"Example elements output after reverse: \");
//printing the array after reverse
for(int i=0;i<size;i++){
Console.Write(\"{0} \", arr[i]);
  
}
  
}
  
//method to reverse array
public static void PrintReverse(string[] array){
for (int i = 0; i < array.Length / 2; i++)
{
string tmp = array[i];//store the value in temp var
array[i] = array[array.Length - i - 1];
array[array.Length - i - 1] = tmp;
}
}
}
------------output----------
Enter the array size
4   
Enter the elements
the   
soggy   
can   
was   
Example elements input: the soggy can was
Example elements output after reverse: was can soggy the
-------------output-----------

Write a C# program with a method, public static void PrintReverse(string[] array). Than print elements in array from right to left. Example elements input: \
Write a C# program with a method, public static void PrintReverse(string[] array). Than print elements in array from right to left. Example elements input: \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site