-1

Create a string function ReturnOdd(int [] tab, int i), which returns only odd numbers from an array using recursion, e.g 3,9,7,5,21,23

static ReturnOdd(int [] tab, int i)
{
    if (tab.Length == 0 || i >= tab.Length)
            return "";

    if (i == tab.Length - 1)
    {
        if (tab[i] % 2 != 0)
            return Convert.ToString(tab[i]);
        else
            return "";
    }

    if (tab[i] % 2 != 0)
        return Convert.ToString(tab[i] + "," + ReturnOdd(tab, i + 1));
    else
        return Convert.ToString(ReturnOdd(tab, i + 1));
}

It's fine when the last value in array is odd, but when it's even funcion displays comma at the end of the lane e.g 1,5,765,3,675,55,811,

Could someone help me with getting rid of the comma at the end?

Andy
  • 3,997
  • 2
  • 19
  • 39
  • 1
    The return type of `ReturnOdd` is missing – Andy Nov 07 '18 at 16:03
  • You have error here: `tab[i] + "," + ReturnOdd(tab, i + 1)`. You are adding comma event if following `ReturnOdd` call return empty string due to lack odd numbers. – user4003407 Nov 07 '18 at 16:04
  • edited my answer, check again – Andreas Nov 07 '18 at 16:14
  • This is probably not a great use of recursion. Consider just walking the array and `Append`ing your numbers to a StringBuider. And, instead of putting the comma after the digit, put it before. It's much easier to check if something is the first instance of something (and therefore doesn't need a comma) than if it's the last. If this wasn't closed, I'd answer it with some pretty simple code. – Flydog57 Nov 07 '18 at 16:29
  • i guess the task was to learn recursion. – Andreas Nov 07 '18 at 16:33
  • Yes, it was. Andreas answer was very helpful. Thanks – Marta Mazurkiewicz Nov 07 '18 at 16:36
  • I hope that you teacher went over the problems with recursion. What makes something a good candidate for recursion and what doesn't. Maybe he/she went over tail call recursion optimizations, etc. – Flydog57 Nov 07 '18 at 16:39

2 Answers2

0
static string ReturnOdd(int[] tab, int i)
    {
        if (tab.Length == 0 || i >= tab.Length)
            return "";

        if (i == tab.Length - 1)
        {
            if (tab[i] % 2 != 0)
                return Convert.ToString(tab[i]);
            else
                return "";
        }

        if (tab[i] % 2 != 0)
        {
            string s = ReturnOdd(tab, i + 1);
            if (!String.IsNullOrEmpty(s))
                s = "," + s;
            return Convert.ToString(tab[i] + s);
        }
        else
            return Convert.ToString(ReturnOdd(tab, i + 1));
    }

I'd still do it like this:

    static String ReturnOdd(int[] tab)
    {
        StringBuilder sb = new StringBuilder();
        foreach (int i in tab)
        {
            if (i % 2 == 1)
                sb.Append($"{i},");
        }
        return sb.ToString().TrimEnd(',');
    }
Andreas
  • 828
  • 4
  • 15
-1

You can check the string to see if it ends with a comma, and if it does, you can take a substring starting with the first character and taking all but the last one:

string s = "8,2,3,4,5,"; // just a sample - this would be your list of odd numbers
if (s.EndsWith(","))
    s = s.Substring(0, s.Length - 1); // leave off the last character
Dave Smash
  • 2,941
  • 1
  • 18
  • 38