hey everyone in daniweb. i have a problem here.

let say i have 2 arrays one is a and second is b. how do i compare each of them and insert to new array c?

i do it like this

Dim i As Integer = 0
Dim arr(26) As Char
For Each c As Char Ina
arr(i) = c
i += 1

Dim b As String = "RAHMAN"
Dim l As Integer = b.Length - 1
Dim newArray(l) As Char
i = 0
For Each c As Char In b
newArray(i) = c
i += 1

the problem is i want to store in new array cArray() = 'R', 'A', 'H', 'M', 'N', 'B', 'C' ,and so on.....

how do i do this? this is for my assignments on permutation cipher program.

Recommended Answers

All 10 Replies

how do i compare each of them and insert to new array c

You'll have to be clearer on what you are trying to do. You have a string (not an array as you stated), a, that contains "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and a string (again, not an array), b that contains "RAHMAN". How do you determine what you want to put into array (this time you are correct), c?

i am trying to store character from the string to array. let say for the first one
and so on

then the same for b

then from both array i want to create new array the contains both a + b, let say
and so on till c(25)='z'

so how i do this? there is comparison to array when there is same character then it will proceed to next character.

and what am i trying to do is the keyword substitution cipher. thanks again

You still haven't said how to combine the two strings. Why does c get "rahmn", then "bcde..."? Why not c = "rahmanabcdef...". Unless I know what the reasoning is I can't help you with the code.

what is i am trying to is c should be the combination of a and b. but c has a few condition.
if b(0) not same as a(0) then c(0) = b(0)
then it checks if b(0)=a(0) then c(0) = a(0)

but if for example character a in array already exist before, then it will not be displayed again.

that is why it should be like rahmnbcdefgijklopqstuvwxyz in array c.

what was happening it should check also for already exist character so it wont duplicate in array c.

i am in blur about this. thanks again for helping me.

Note - the following code assumes that len(b) <= len(a)

Dim a As String = "abcdefghijklmnopqrstuvwxyz"
Dim b As String = "rahman"
Dim c As String = ""
Dim ch As Char

For i As Integer = 0 To Len(a) - 1

    'If there are still chars left in b then pick a char from a (if a=b)
    'or from b (if a <> b). If there are no more chars in b then just take
    'the next char from a

    If i < Len(b) Then
	ch = IIf(a(i) = b(i), a(i), b(i))
	ch = a(i)
    End If

    'If the selected char is not already in c then add it to c

    If InStr(c, ch) = 0 Then
	c += ch
    End If



thanks for your reply. it is slmost the same as what i want.

but the output is rahmngjiklpqstuvwxyz. should the result is rahmnbcdefgijklopqstuvwxyz.

there is missing bcdef.

how do i fix this? and can you explain a bit bit how your code works?


First let's get the algorithm correct. We start by comparing a(0) to b(0). They are not equal so c gets b(0). What are the next two things to compare? Do we compare a(0) to b(1) or a(1) to b(1)? When do we increment the index into a? If we are never going to increment the index for string "a" then what is the point of comparing chars in string "a" to chars in string "b"?

oh. then is my logic got wrong. sorry. i thought it should be comparing while inserting strings to c.

so lets say if i want to do it like the output is rahmngjiklpqstuvwxyz how am i going to write the logic.

thanks again.

I'm not saying that rahmnbcdefgijklopqstuvwxyz is the wrong answer. I'm saying that if rahmnbcdefgijklopqstuvwxyz is the right answer then you will have to explain why it is the right answer. In other words, what is the algorithm you are using to generate c? If you don't know the answer to that then you can't write a program to do it.

Well then, there is my logic goes. i may have mistakes here.

but this one maybe useful. i try to find the logic from the code you gave me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.