0

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;
        }
    }
1
  • Why don't you simply pass input string to your function fnReverse it should work
    – Ankit
    Commented Sep 10, 2017 at 18:04

2 Answers 2

2
    string strIn = "Welcome to the world";
    string strTmp = "";
    string strOut = "";

    for (int i=strIn.Length-1; i>-1; i--)
    {
        if (strIn[i] == ' ')
        {
            strOut = strTmp + " " + strOut;
            strTmp = "";
        }
        else
        {
            strTmp += strIn[i];
        }   
    }
    strOut = strTmp + " " + strOut;

Gives the result "emocleW ot eht dlrow"

3
  • this is just same as my answer Commented Sep 10, 2017 at 18:31
  • @AshkanMobayenKhiabani It does appear to be the same as yours, although this appears to have been posted before yours was edited to use this method. I would suggest that you both arrived at the same answer independently.
    – YowE3K
    Commented Sep 10, 2017 at 19:28
  • @AshkanMobayenKhiabani Yes sorry, it was the last thing I posted before I logged off and then saw that you had edited yours to be the same. Great minds think alike and all that..
    – Deolus
    Commented Sep 11, 2017 at 7:28
1
 string input = Console.ReadLine();
            string result = "";
            string tmp = "";
            for (int i = input.Length - 1; i >= 0; i--)
            {
                if (input[i] == ' ')
                {
                    result =  tmp + " " + result;
                    tmp = "";
                }
                else
                    tmp += input[i];
            }
            result = tmp + " " + result;
            Console.WriteLine(result);

Here is the DEMO

5
  • Thanks for your response but if i consider over all program i should be using only one loop but in my code i have used 2 loops. Commented Sep 10, 2017 at 18:04
  • can you tell me what are these two loops? I don't get it. there is only one loop for reversing the string Commented Sep 10, 2017 at 18:05
  • @AshkanMobayenKhiabani I think he is talking about the foreach that loops each word.
    – Guilherme
    Commented Sep 10, 2017 at 18:07
  • you can not have it with single loop. you may hide the loop for example using linq query but still it will be there Commented Sep 10, 2017 at 18:08
  • @Anandkumar now I get what you mean. please have a look at my edited answer Commented Sep 10, 2017 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.