Good code, few suggestions.
Instead of having to check each and every time to make sure you can add a ","
or not, add it for all the cases, use String.TrimEnd to remove the extra leading ","
var names = s.Split(",");
string temp = "";
for (int i = 0; i < names.Length; i++)
{
for (int j = names[i].Length - 1; j >= 0; j--)
{
temp += names[i].ElementAt(j);
}
temp += ",";
}
return temp.TrimEnd(',');
With that change, we have the ability to foreach loop to further improve the readability of the code.
string temp = "";
foreach (var name in s.Split(','))
{
for (int j = name.Length - 1; j >= 0; j--)
{
temp += name.ElementAt(j);
}
temp += ",";
}
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 = "";
foreach (var name in s.Split(','))
{
char[] NameArray = name.ToCharArray();
Array.Reverse(NameArray);
temp = temp + new string(NameArray) + ",";
}
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(",");
}
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 ""
Benchmarks
Your original code
static public string Reverse(string s)
{
var names = s.Split(",");
string temp = "";
for (int i = 0; i < names.Length; i++)
{
for (int j = names[i].Length - 1; j >= 0; j--)
{
temp += names[i].ElementAt(j);
}
temp += i == names.Length - 1 ? "" : ",";
}
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) + ",";
}
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(",");
}
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<string, string> Function, string names, int iterations, string type)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0;i < iterations;i++)
Function(names);
watch.Stop();
var elapsed = watch.ElapsedMilliseconds;
Console.WriteLine($"Time taken for {type}: {elapsed} ms");
}
static void Main()
{
string names = "Jack,Dylan,Bobby,Peter";
const int iterations = 1000000;
Perform(Reverse, names, iterations, "Original");
Perform(Reverse2, names, iterations, "New");
Perform(Reverse3, names, iterations, "New2");
}
Here are the results
/*
" Release " 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
return temp
? :) \$\endgroup\$