Click here to Skip to main content
15,922,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have tried a lot but am not getting a proper reversed array in second array.

What I have tried:

VB
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
Posted
Updated 18-Oct-16 4:38am
v2

Quote:
I know the basics. I have been working and studying in this field for total of 10 years now.
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.
 
Share this answer
 
Comments
Member 12800385 18-Oct-16 10:43am    
I was damn busy with other projects.
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).
 
Share this answer
 
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.
 
Share this answer
 
Comments
Member 12741660 28-Sep-16 8:29am    
I have done that in the code.
OriginalGriff 28-Sep-16 8:35am    
No, you aren't.
You are copying from the same array as you copy to, because of this line:

charaR = chara

That copies the reference from one variable to teh otehr, so they are both "pointing" at the same data in memory.
It's like cars: You have "your car", and your new girlfriend has "her car". She moves in with you, and you decide that one can go, and sell one. Now both "your car" and "her car" refer to the same vehicle - if you crash "your car" you write off "her car" as well!
Member 12741660 28-Sep-16 9:57am    
chara and charaR are both declared separately.
chara has original string.
charaR is supposed to take the data of chara in reverse order.
You see ?
Do one thing run the code.
Actually it is doing half the task.
World is getting reversed fine as dlroW.
But not hello.
Just run this code once you will understand better.
OriginalGriff 28-Sep-16 10:11am    
The variables are declared separately, but that line of code means they share a common value - and hence a reference to the same area of memory.
You only allocate one area of memory - by converting the string "Hello world" to a character array - and you use the same memory for both variables.
Try it: add these two lines after the assignment:

charaR = chara
chara(0) = 'X'
charaR(0) = 'Y'

then look at the content of "both" arrays.
You will hopefully see what I mean.
After months I got ample time to actually even look at the code.
I have rectified my error.
This code works perfect.
VB.NET
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 = CType(str, Char())
        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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900