我想在循环中以相反的格式打印字符串:
输入:我正在学习c#
输出:c#正在学习
不应使用Split函数和反向函数,它仅与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();
最佳答案
没有拆分和反向,我会稍微修改您的逻辑以实现您所需要的
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!");
}
}
}
输入:“这是mystring”
输出:“ mystring是这个”
关于c# - 在C#中使用for循环的完整句子的反词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48223203/