Reverse word of full sentence

后端 未结 16 1886
眼角桃花
眼角桃花 2020-12-06 20:04

I want to print string in reverse format:

Input: My name is Archit Patel

Output: Patel Archit is name My.

I\'ve tied the

相关标签:
16条回答
  • 2020-12-06 20:48

    this.lblStringReverse.Text = Reverse(this.txtString.Text);

    private int NoOfWhiteSpaces(string s)
            {
                char[] sArray = s.ToArray<char>();
                int count = 0;
                for (int i = 0; i < (sArray.Length - 1); i++)
                {
                    if (sArray[i] == ' ') count++;
                }
                return count;
            }
            private string Reverse(string s)
            {
                char[] sArray = s.ToArray<char>();
                int startIndex = 0, lastIndex = 0;
                string[] stringArray = new string[NoOfWhiteSpaces(s) + 1];
                int stringIndex = 0, whiteSpaceCount = 0;
    
                for (int x = 0; x < sArray.Length; x++)
                {
                    if (sArray[x] == ' ' || whiteSpaceCount == NoOfWhiteSpaces(s))
                    {
                        if (whiteSpaceCount == NoOfWhiteSpaces(s))
                        {
                            lastIndex = sArray.Length ;
                        }
                        else
                        {
                            lastIndex = x + 1;
                        }
                        whiteSpaceCount++;
    
                        char[] sWordArray = new char[lastIndex - startIndex];
                        int j = 0;
                        for (int i = startIndex; i < lastIndex; i++)
                        {
                            sWordArray[j] = sArray[i];
                            j++;
                        }
    
                        stringArray[stringIndex] = new string(sWordArray);
                        stringIndex++;
                        startIndex = x+1;
                    }
    
                }
                string result = "";
                for (int y = stringArray.Length - 1; y > -1; y--)
    
                    {
                        if (result == "")
                        {
    
                            result = stringArray[y];
                        }
                        else
                        {
                            result = result + ' ' + stringArray[y];
                        }
                }
                return result;
            }
    
    0 讨论(0)
  • 2020-12-06 20:50

    Here is an easy way

            public static string MethodExercise14Logic(string str)
            {
                string[] sentenceWords = str.Split(' ');
                Array.Reverse(sentenceWords);
                string newSentence = string.Join(" ", sentenceWords);
                return newSentence;
            }

    1. You first create an array called sentence word and use the split method to populate the array with all your words. Note that space is used as a delimiter, telling the split method when exactly to split.
    2. You then use the Reverse method in the Array class to reverse the complete array.
    3. You then use the Join method of the string class to join your now reversed array into a string called newSentence.
    4. Finally the method then returns the newSentence string.
    0 讨论(0)
  • 2020-12-06 20:54

    "please send me the code of this program."

    Okay ...

    using System;
    using System.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
            string text = "My name is Archit Patel";
    
            Console.WriteLine(string.Join(" ", text.Split(' ').Reverse()));
        }
    }
    

    Now: what have you learned?

    Also, as Guffa points out, for versions below .Net 4.0 you'll need to add .ToArray() since string.Join doesn't have the correct overload in those versions.

    0 讨论(0)
  • 2020-12-06 20:55

    I appreciate Rob Anderson answer, but it will reverse complete sentence, Just editing that

    string s = "My name is Archit Patel";
    
            string[] words = s.Split(' ');
            StringBuilder sb = new StringBuilder();
    
            for (int i = words.Length - 1; i >= 0; i--)
            {
                sb.Append(words[i]);
                sb.Append(" ");
            }
    
            Console.WriteLine(sb);
    

    O/P will be "Patel Archit is name My"

    0 讨论(0)
提交回复
热议问题