-1

I want to reverse an array with console.ReadLine(), but I'm stuck.
Here is statement of problem.

Give an array of 10 natural numbers. Displays the numbers in the string in reverse order.

The numbers in the string are given on a single line and are separated by a space. The application will display the numbers on a single line, separated by a space between them.

Example:

For input data:

1 2 3 4 5 6 7 8 9 10

The console will display:

10 9 8 7 6 5 4 3 2 1

here is my code:

string number = Convert.ToString(Console.ReadLine());

List<string> originalList = new List<string>();

for (int i = 0; i < 1; i++)
{
    originalList.Add(number);
}
foreach (string item in originalList)
{
    Console.Write(item + " ");
}
originalList.Reverse();

foreach (string item in originalList)
{
    Console.WriteLine(item + " ");
}
Console.WriteLine();
IrAM
  • 1,720
  • 5
  • 18

4 Answers4

0

The numbers in the string are given on a single line and are separated by a space.

The following code is incorrect because Console.ReadLine does not stop reading when it encounters whitespace. It will consume the entire input as one entry in originalList.

string number = Convert.ToString(Console.ReadLine());

List<string> originalList = new List<string>();

for (int i = 0; i < 1; i++)
{
    originalList.Add(number);
}

// originalList is now [ "1 2 3..." ];

Instead, you should look at String.Split.

string input = Console.ReadLine();

List<string> originalList = input.Split(' ').ToList();

// originalList is now [ "1", "2", "3", ... ];

You can now use List.Reverse and String.Join to get your output.

originalList.Reverse();

string output = string.Join(' ', originalList);

Console.WriteLine(output);
D M
  • 5,769
  • 4
  • 12
  • 27
0

Need either string.Split (' ') or a for loop or a while loop

Using string.Split (' ')

string [] splitArray = number.Split (' ');
for (int i = 0; i < splitArray.Length; i++)
{
    originalList.Add(splitArray [i]);
}
//rest of the code would be same

For loop

String readvalues="";
for (int i=0;i <number.Length;i++)
{
    if (i==number Length-1)
    {
        readvalues += number [i];
    }
    if (number[i]==" " || i==number.Length-1)
    {
        originalList.Add (readvalues);
        readvalues="";
    }
    else
    {
        readvalues += number [i];
    }
}
//rest of the code would be same

While loop

int index=0;
String readvalues="";
while (index < number.Length)
{
    if (index==number.Length-1)
    {
        readvalues += number [index];
    }
    if (number[index]==" " ||index==number.Length-1)
    {
        originalList.Add (readvalues);
        readvalues="";
    }
    else
    {
        readvalues += number [index];
    }
    index++;
}
//rest of the code would be same
novice in DotNet
  • 771
  • 1
  • 9
  • 21
0

You can try querying the initial string with a help of Linq (Reverse):

Code:

  using System.Linq;

  ...

  // For Test (comment it out for release)
  string source = "1 2 3 4 5 6 7 8 9 10";

  // Uncomment for release 
  // string source = Console.ReadLine();

  string result = string.Join(" ", source
    .Split(' ', StringSplitOptions.RemoveEmptyEntries)
    .Reverse());

  Console.WriteLine(result);

Outcome:

  10 9 8 7 6 5 4 3 2 1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

I think, I have a solution for you, what you exactly ask for. Please check the code and let me know =>

void Test()
{
        string number = Convert.ToString(Console.ReadLine());
        List<string> originalList = new List<string>();

        if(!string.IsNullOrEmpty(number) && !string.IsNullOrWhiteSpace(number) )
        {
            originalList=number.Split(" ").ToList();
            originalList.Reverse();
            Console.WriteLine(string.Join(" ", originalList));
        } 
}

Note : Use those three namespaces using System;,using System.Linq;,using System.Collections.Generic;

Srijon Chakraborty
  • 2,007
  • 2
  • 7
  • 20