Converting a string into an array and returning the values spelled in reverse I must create a function that accepts a string of names all separated by a comma.
Example: &quot;Jack,Matthew,John,Sam&quot;
And return the string of names in the same order but spelled in reverse.
Expected output: &quot;kcaJ,wehttaM,nhoJ,maS&quot;
I completed the function with an inner for loop but I would like to know if maybe running a single for loop and inside using a Linq function or something similar, would have been better?
public string Reverse(string s)
{
    var names = s.Split(&quot;,&quot;);
    string temp = &quot;&quot;;

    for (int i = 0; i &lt; names.Length; i++)
    {
        for (int j = names[i].Length - 1; j &gt;= 0; j--)
        {
            temp += names[i].ElementAt(j);
        }
        temp += i == names.Length - 1 ? &quot;&quot; : &quot;,&quot;;
    }

    return temp;
}

Anyway I found this problem very simple even though I tried taking my time on it, I found this solution almost immediately so I went with it. I am thinking maybe if I spent more time on it then I could have implemented this with some sort of Linq function to reverse the spelling of the names and removed the inner loop.
Really any suggestions for improving this small function is appreciated.
Good code, few suggestions.

Instead of having to check each and every time to make sure you can add a &quot;,&quot; or not, add it for all the cases, use String.TrimEnd to remove the extra leading &quot;,&quot;
            var names = s.Split(&quot;,&quot;);
            string temp = &quot;&quot;;

            for (int i = 0; i &lt; names.Length; i++)
            {
                for (int j = names[i].Length - 1; j &gt;= 0; j--)
                {
                    temp += names[i].ElementAt(j);
                }
                temp += &quot;,&quot;;
            }
            return temp.TrimEnd(',');

With that change, we have the ability to foreach loop to further improve the readability of the code.
            string temp = &quot;&quot;;
            foreach (var name in s.Split(','))
            {
                for (int j = name.Length - 1; j &gt;= 0; j--)
                {
                    temp += name.ElementAt(j);
                }
                temp += &quot;,&quot;;
            }
            return temp.TrimEnd(',');

That's good. Although we can improve this further by using a better (faster and cleaner) method to reverse the individual names, and that is to use String.ToCharArray and Array.Reverse
The idea is to convert the string into a character array, use the inbuilt Reverse method to reverse it, add it to temp.
            string temp = &quot;&quot;;
            foreach (var name in s.Split(','))
            {
                char[] NameArray = name.ToCharArray();
                Array.Reverse(NameArray);

                temp = temp + new string(NameArray) + &quot;,&quot;;

            }
            return temp.TrimEnd(',');

You can also use StringBuilder which is slightly faster
            StringBuilder reversed = new StringBuilder();

            foreach (var word in str.Split(','))
            {
                char[] singlesentence = word.ToCharArray();
                Array.Reverse(singlesentence);
                reversed.Append(singlesentence);
                reversed.Append(&quot;,&quot;);
            }
            return reversed.ToString().TrimEnd(',');

Nitpick:

Prefer using a better name like reversed compared to temp, it tells more to the reader.

Use string.Empty and not &quot;&quot;



Benchmarks
Your original code
        static public string Reverse(string s)
        {
            var names = s.Split(&quot;,&quot;);
            string temp = &quot;&quot;;

            for (int i = 0; i &lt; names.Length; i++)
            {
                for (int j = names[i].Length - 1; j &gt;= 0; j--)
                {
                    temp += names[i].ElementAt(j);
                }
                temp += i == names.Length - 1 ? &quot;&quot; : &quot;,&quot;;
            }
            return temp;
        }


New
        static public string Reverse2(string s)
        {
            
            string reversed = string.Empty;
            foreach (var name in s.Split(','))
            {
                char[] NameArray = name.ToCharArray();
                Array.Reverse(NameArray);

                reversed += new string(NameArray) + &quot;,&quot;;

            }
            return reversed.TrimEnd(',');
        }


New2
        static public string Reverse3(string str)
        {
            StringBuilder reversed = new StringBuilder();

            foreach (var word in str.Split(','))
            {
                char[] singlesentence = word.ToCharArray();
                Array.Reverse(singlesentence);
                reversed.Append(singlesentence);
                reversed.Append(&quot;,&quot;);
            }
            return reversed.ToString().TrimEnd(',');
        }


Note that I have static to use the function without an object reference.
I will use the following method to call the two functions multiple times, and benchmark it.
static void Perform(Func&lt;string, string&gt; Function, string names, int iterations, string type)
{

    var watch = System.Diagnostics.Stopwatch.StartNew();
    for (int i = 0;i &lt; iterations;i++)
        Function(names);

    watch.Stop();

    var elapsed = watch.ElapsedMilliseconds;
    Console.WriteLine($&quot;Time taken for {type}: {elapsed} ms&quot;);
}

        static void Main()
        {
            string names = &quot;Jack,Dylan,Bobby,Peter&quot;;
            const int iterations = 1000000;

            Perform(Reverse,  names, iterations, &quot;Original&quot;);
            Perform(Reverse2, names, iterations, &quot;New&quot;);
            Perform(Reverse3,  names, iterations, &quot;New2&quot;);
       
        }

Here are the results
/*
&quot; Release &quot; Configuration

 Iterations  |   Original  |   New     |   New2
-------------+-------------+-----------+--------
 10 000      |    29   ms  |    5   ms |   5   ms
-------------+-------------+-----------+--------
 100 000     |    226  ms  |    54  ms |   43  ms
-------------+-------------+-----------+--------
 1 000 000   |    1763 ms  |    442 ms |   405 ms


*/

Try it here