换弦的问题 [英] Problem with reversing string

查看:104
本文介绍了换弦的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码应获取一个字符串,然后在输出中返回该字符串的反向字符串,但它有问题,我想知道您是否可以检查它并说我应该做什么,非常感谢您.

This code should be get a string then return reverse that string in output but it has problem I want to know can you check it and say wat should I do ecxatlly,thank u so much.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace prog_83_reshteh_method_
{
    class Program
    {
        public void reshteh(string x)
        {
            Console.WriteLine("enter a string");
            string str = Console.ReadLine();
            int y= Convert.ToInt32(str);

            for (int i = str.Length - 1; i >= 0; i--)
            {
                Console.WriteLine(str.Substring(i,1));
            }

        }
        static void Main(string[] args)
        {
            Console.WriteLine("enter a string");
            string str = Console.ReadLine();
            int y = Convert.ToInt32(str);

            Program obj = new Program();
            obj.reshteh(x);

        }
    }
}

推荐答案


  1. 考虑使用扩展方法 [
  2. 如何构建字符串取决于您,可以使用StringBuilder 或仅添加到现有字符串中,甚至创建一个反向的char数组并从中创建一个字符串,这取决于您的需要.

  1. Consider using an extension method[^] on the string called Reverse(), if you are using .net 3.5 or above. If below 3.5 create a method string Reverse(string original) {...}
  2. As for the alogrithm "my string"[1] returns the character ''y''. So all you have to do is to iterate over the array, if you iterate forwards you need to fill the built string backwards and vice versa.
  3. How you build the string is up to you, you could use a StringBuilder or just add to an existing string or even create an reversed array of char and create a string from that, depends what you need.


那里这里有很多错误.

1)复制代码:如果希望您的reshteh方法使用"x"参数,请从reshteh方法中删除以下代码块,并更新for块以使用x变量而不是str变量.
There are many things wrong here.

1) Duplicate code: If you want your reshteh method to work using the "x" argument, then remove the following code block from the reshteh method and update the for block to use the x variable instead of the str variable.
Console.WriteLine("enter a string");
string str = Console.ReadLine();
int y= Convert.ToInt32(str);

for( int i = x.Length -1; i >= 0; i-- ) ...


字符串只是字符数组,因此无需使用SubString.

A string is simply an array of characters, there is no need to use SubString for this.

public void reshteh(string x)
{
  Console.WriteLine("enter a string");
  string str = Console.ReadLine();

  for (int i = str.Length; i >= 0; i--)
  {
    Console.WriteLine(str[i]);
  }

}


这篇关于换弦的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆