如何在VB.NET中使用字符数组来反转字符串,条件是反向数组存储在单独的字符数组中。不使用反向和其他.NET函数。 [英] How to reverse a string by using character array in VB.NET, condition being that reversed array is stored in separate character array. No using of reverse and other .NET functions.

查看:362
本文介绍了如何在VB.NET中使用字符数组来反转字符串,条件是反向数组存储在单独的字符数组中。不使用反向和其他.NET函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很多但是没有在第二阵列中获得正确的反转阵列。



我尝试过的事情:



I have tried a lot but am not getting a proper reversed array in second array.

What I have tried:

Module Module1

    Sub Main()
        Dim str As String
        Dim chara() As Char
        Dim charaR() As Char
        Dim len As Integer
        Dim j As Integer
        str = "Hello World"
        chara = CType(str, Char())
        len = str.Length
        j = len
        j = j - 1
        charaR = chara
        Console.WriteLine(str)
        Console.WriteLine("String Length: " & str.Length)
        Console.WriteLine()
        Console.WriteLine("Normal array:")
        For i = 0 To len - 1
            Console.Write(chara(i))
        Next
        Console.WriteLine()
        For i = 0 To len - 1
            Console.WriteLine(chara(j))
            charaR(i) = chara(j)
            'Console.Write(charaR(i))
            j -= 1
        Next
        Console.WriteLine()
        For i = 0 To len - 1
            Console.Write(charaR(i))
        Next
        Console.WriteLine()
        Console.ReadLine()
    End Sub

End Module

推荐答案

引用:

我知道基础知识。我已经在这个领域工作和学习了10年。

I know the basics. I have been working and studying in this field for total of 10 years now.

你在这10年里做了什么?这个问题是第一年的水平!



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候您接近一个错误。



建议:拿一张纸并尝试手工完成,您的程序应该使用相同的程序。

What have you done in those 10 years ? this problem is first year level !

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.


看起来你搞砸了引用。字符应该从原始字符串复制到第一个数组( chara ),然后从后者(将它们反转)复制到第二个( charaR )。
It looks you are messing up with references. The characters should be copied from the original string to first array (chara) and then copied from the latter (reversing them) to the second one (charaR).


这是作业,所以没有代码!

最简单的方法是创建一个char与原始数组长度相同的数组,然后使用For循环遍历原始字符串(或数组) - 从(Length - 1)向下运行到零。在开始循环之前,添加一个从零开始的输出索引变量。在循环内部,将字符从原始字符复制到输出,并增加输出索引。

完成。
This is homework, so no code!
Simplest way is to create a char array of the same length as the original, then loop through the original string (or array) using a For loop - running from the (Length - 1) downwards to zero. Before you start the loop, add an "Output index" variable which starts from zero. Inside the loop, copy the character from the original to the output, and increment the output index.
Done.


这篇关于如何在VB.NET中使用字符数组来反转字符串,条件是反向数组存储在单独的字符数组中。不使用反向和其他.NET函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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