在c#中使用for循环反转完整句子的单词

时间:2018-01-12 09:31:08

标签: c#

我想在for循环中以反向格式打印字符串:

输入:我正在学习c#

输出:c#学习我是

不应使用分割功能和反向功能,只能使用forloop。

for (int i = m.Length - 1; i >= 0; i--)
{
    b[j]=a[i];
    j++;
    if(a[i]==' '|| a[i]==0)
    {
       for (int x = b.Length - 1; x >= 0; x--)
       {
           c[k] = b[x];
           Console.Write(c[k]);
           k++;
       }
    }

} Console.ReadKey();

5 个答案:

答案 0 :(得分:5)

你必须在句子中创建一个单词数组:

var words = input.Split(' ');

然后你只需从头到尾遍历上面的数组:

for(int i=words.Length-1; i>=0; i--)
{
    Console.Write(words[i]+" ");
}

答案 1 :(得分:3)

使用LINQ和字符串方法,您可以简化它:

var reversedWords = input.Split().Reverse(); // Split without parameters will use space, tab and new-line characters as delimiter
string output = string.Join(" ", reversedWords); // build reversed words, space is delimiter

答案 2 :(得分:1)

使用Stack<Queue<char>>

嘿,如果你想展示你对数据结构的了解,请使用队列和堆栈!这也是一个非常简洁的答案。

你希望句子相对于单词而言是LIFO,而不是单词中的字母,因此你需要一个堆栈(它是LIFO)的队列(它们是FIFO)。您可以利用stringqueue<char>stack<char>都展示IEnumerable<char>这一事实,因此很容易来回转换;一旦你在数据结构中排序了所有字符,你就可以使用SelectMany()将整个事物提取为字符数组,你可以将它传递给字符串构造函数以获得最终答案。

此解决方案根据需要不使用Split()Reverse()函数。

public static string ReverseSentence(string input)
{
    var word = new Queue<char>();
    var sentence = new Stack<IEnumerable<char>>( new [] { word } );
    foreach ( char c in input )
    {
        if (c == ' ')
        {
            sentence.Push( " " );              
            sentence.Push( word = new Queue<char>() ); 
        }
        else
        {
            word.Enqueue(c);
        }
    }
    return new string( sentence.SelectMany( w => w ).ToArray() );
}

用法:

public void Test()
{
    var input = "I'm Learning c#";
    var output = ReverseSentence(input);
    Console.WriteLine(output);
}

输出:

c# Learning I'm

DotNetFiddle

答案 3 :(得分:0)

这是一个简单的控制台应用程序,用于您的问题

不使用ReverseSplit方法(仅for循环)

class Program
{
    static void Main()
    {
        string input = "I'm learning C#";
        string[] result = new string[3];

        int arrayIndex = 0;
        string tempStr = "";

        // Split string 
        for (int i = 0; i < input.Length; i++)
        {
            tempStr += input[i].ToString();   
            if (input[i] != ' ')
            {
                result[arrayIndex] = tempStr;
            }
            else
            {
                tempStr = "";
                arrayIndex++;
            }
        }

        // Display Result
        for (int i = result.Length - 1; i >= 0; i--)
        {
            System.Console.Write(result[i] + ' ');
        }
        System.Console.WriteLine();
    }
}

Ctrl + F5运行程序

输出:C#学习I&#39; m

答案 4 :(得分:0)

没有拆分和反转,我会稍微修改你的逻辑以实现你需要的东西

namespace MyNamespace
{
    public class Program
    {
        public static void Main(string[] args)
        {
          var m="this is mystring";

          var b = new char [m.Length];


          var j=0;
            //Your code goes here
          for (int i = m.Length - 1; i >= 0; i--)
          {
              b[j]=m[i];
              j++;
              if(m[i]==' ' || i==0)
             {
                if(i==0)
                {
                   b[j]=' ';
                }
                for (int x = b.Length - 1; x >= 0; x--)
               {

                   Console.Write(b[x]);

              }
              b=new char[m.Length];
              j=0;
           }

        } 
          Console.ReadKey();
           Console.WriteLine("Hello, world!");
        }
    }
}

输入:&#34;这是mystring&#34;

输出:&#34; mystring就是这个&#34;