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?