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 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"

Sign up to request clarification or add additional context in comments.

3 Comments

this is just same as my answer
@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.
@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..
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 Comments

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.
can you tell me what are these two loops? I don't get it. there is only one loop for reversing the string
@AshkanMobayenKhiabani I think he is talking about the foreach that loops each word.
you can not have it with single loop. you may hide the loop for example using linq query but still it will be there
@Anandkumar now I get what you mean. please have a look at my edited answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.