I have tried writing the logic for reversing each word in a string with single loop but I didn't get it to work. Can you please provide the logic to reverse every word in a string using single loop and without using reverse function.
Input:
Welcome to the world
Output:
emocleW ot eht dlrow
My Logic with two loops:
class Program
{
static void Main(string[] args)
{
string input = string.Empty;
input = Console.ReadLine();
string[] strarr=input.Split(' ');
StringBuilder sb = new StringBuilder();
foreach (string str in strarr)
{
sb.Append(fnReverse(str));
sb.Append(' ');
}
Console.WriteLine(sb);
Console.Read();
}
public static string fnReverse(string str)
{
string result = string.Empty;
for (int i = str.Length-1; i >= 0; i--)
result += str[i];
return result;
}
}