E
E
Error 4042022-03-10 12:19:12
C++ / C#
Error 404, 2022-03-10 12:19:12

The code doesn't work. Errors CS0161 and CS1513. What to do?

using System;
using System.Linq;

namespace project {
    class Program {
        public static void Main() {
            Console.WriteLine(Reverse("This is an example!"));
        }
        public static string Reverse(string text) {
            string[] arr = text.Split();
            for(int i = 0; i != arr.Length; i++) {
                char[] array = arr[i].ToCharArray();
                Array.Reverse(array);
                arr[i] = new String(array);
            string result = string.Join(" ", arr);
            return result;
        }
    }
}


The code doesn't work. Checked several times, everything is correct.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Bannikov, 2022-03-10
@404err0r404

1. What should this line do?
string[] arr = text.Split();
2. Show where the cycle starts and ends

Spoiler if too lazy to play a guessing game

public static string Reverse(string text) {
      string[] arr = text.Split();
      for(int i = 0; i != arr.Length; i++) {
          char[] array = arr[i].ToCharArray();
          Array.Reverse(array);
          arr[i] = new String(array);
      } // Забыл вот эту фигурную скобку
      string result = string.Join(" ", arr);
      return result;
  }

A
Alexander Ananiev, 2022-03-10
@SaNNy32

Check the correct placement of brackets {}

V
Vladimir Korotenko, 2022-03-10
@firedragon

so there was no parenthesis,
if the string length is 0 then nothing was returned to which the compiler cursed, well, look at the output of the ide, it shows where the error is

using System;

namespace project
{
    class Program
    {
        public static void Main()
        {
            Console.WriteLine(Reverse("This is an example!"));
        }

        public static string Reverse(string text)
        {
            var arr = text.Split();
            for (var i = 0; i != arr.Length; i++)
            {
                var array = arr[i].ToCharArray();
                Array.Reverse(array);
                arr[i] = new String(array);
                var result = string.Join(" ", arr);
                return result;
            }

            throw new ArgumentException("String must have size > 0");
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question