I need to create a recursion that will count in array the letter 'x'.
I solved it, but it don't know if this is what they meant that i would do.
I have just started to learn this subject.

If it's not a recursion or it's not a good way to solve my problem i will glad if you will help me to fix it.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = StringArray();
        }

        public static string[] StringArray()
        {
            Console.WriteLine("Array Size:");
            int size = int.Parse(Console.ReadLine());
            string[] a = new string[size];

            string result = "";
            Console.WriteLine("Array Values:");

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = Console.ReadLine();
                result += a[i];
            }

            int sum = 0;
            for (int i = 0; i < result.Length; i++)
            {
                if (result[i] == 'x')
                {
                    sum++;
                }
            }

            Console.WriteLine(sum);
            return a;
        }
    }
}

This isn't recursion at all. Recursion is the act of a function repeatedly calling itself until its task is done.
In your example, if you wanted to fill an array of length 10 with the letter X then you would have a function:

fillArray(string x, int i) {
   a[i] = x;
   i++;
   fillArray(x, i)
}

Of course this needs more code to know when the array is full but hopefully you get the idea. Your function needs to call itself to be considered recursion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.