Reverse word of full sentence

后端 未结 16 1884
眼角桃花
眼角桃花 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:32

    You would need to split the string into words and the reverse those instead of reversing the characters:

    text = String.Join(" ", text.Split(' ').Reverse())
    

    In framework 3.5:

    text = String.Join(" ", text.Split(' ').Reverse().ToArray())
    

    In framework 2.0:

    string[] words = text.Split(' ');
    Array.Reverse(words);
    text = String.Join(" ", words);
    
    0 讨论(0)
  • Use the split method to put it in an array

     string[] words = s.Split(' ');
    

    Then reverse the array with array.reverse

    words = Array.Reverse(words);
    

    Now you can print it with a for-each loop and add spaces

    Hope this helps

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

    Try this without in-built functions

    public string ReverseFullSentence(string inputString)
            {
                string output = string.Empty;
                string[] splitStrings = inputString.Split(' ');
                for (int i = splitStrings.Length-1; i > -1 ; i--)
                {
                    output = output + splitStrings[i]+ " ";
                }
                    return output;
            }
    
    0 讨论(0)
  • 2020-12-06 20:36
        public static string reversewordsInsentence(string sentence)
        {
            string output = string.Empty;
            string word = string.Empty;
            foreach(char c in sentence)
            {
                if (c == ' ')
                {
                    output = word + ' ' + output;
                    word = string.Empty;
                }
                else
                {
                    word = word + c;
                }
            }
            output = word + ' ' + output;
            return output;
        }
    
    0 讨论(0)
  • 2020-12-06 20:39

    You could try:

    string[] words = "My name is Archit Patel".Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    IEnumerable<string> reverseWords = words.Reverse();
    string reverseSentence = String.Join(" ", reverseWords);
    
    0 讨论(0)
  • 2020-12-06 20:40

    This is my solution to an interview question if you need to do it in place. I use one more character for the swap. I assume space is only used as a separator.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("abcd efg hijk");
            Reverse(sb, 0 , sb.Length - 1);
            Console.WriteLine(sb);
            ReverseWords(sb);
            Console.WriteLine(sb);
            ReverseWordOrder(sb);
            Console.WriteLine(sb);
        }
        static void Reverse(StringBuilder sb, int startIndex, int endIndex)
        {
    
            for(int i = startIndex; i <= (endIndex - startIndex) / 2 + startIndex; i++)
            {
                Swap(sb,i, endIndex + startIndex - i);
            }
        }
    
        private static void Swap(StringBuilder sb, int index1, int index2)
        {
            char temp = sb[index1];
            sb[index1] = sb[index2];
            sb[index2] = temp;
        }
    
        static void ReverseWords(StringBuilder sb)
        {
            int startIndex = 0;
            for (int i = 0; i <= sb.Length; i++)
            {
                if (i == sb.Length || sb[i] == ' ')
                {
                    Reverse(sb, startIndex, i - 1);
                    startIndex = i + 1;
                }
            }
        }
        static void ReverseWordOrder(StringBuilder sb)
        {
            Reverse(sb, 0, sb.Length - 1);
            ReverseWords(sb);
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题