Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge two strings in one string

Tags:

string

julia

I am new in julia programming and I don't know if what I wrote is right.

The question is that I have two strings, the first is "dog" the second is "fish", so the merge of these strings should be done one character by one character like this : "dfoigsh".

I am trying to write code to merge two strings but it doesn't work:

str1 = "fuad"
str2 = "hesen"
result = ""
str = ""
merge = str1 * str2

if length(str1)>length(str2)
str= str1
else str=str2
end

for i = 1:length(merge)
        result[i]=str1[i] #fuad , hesenkl, result = fhueasdenkl
        result[j+1]=str2[j]
        j=j+1
        i=i+1
end
println(result)
println(str)
like image 450
Nicholas Avatar asked Sep 15 '25 15:09

Nicholas


2 Answers

Note that in Julia strings are immutable. Therefore in order to merge them you exactly have to use * operator as you did:

julia> str1 = "fuad"
"fuad"

julia> str2 = "hesen"
"hesen"

julia> merge = str1 * str2
"fuadhesen"

If you want to make an exercise with iterating over strings you could create a Vector of characters contained in str1 and str2 and then convert it to a String using join for example like this:

julia> res = Char[]
0-element Array{Char,1}

julia> for c in str1
           push!(res, c)
       end

julia> res
4-element Array{Char,1}:
 'f'
 'u'
 'a'
 'd'

julia> for c in str2
           push!(res, c)
       end

julia> res
9-element Array{Char,1}:
 'f'
 'u'
 'a'
 'd'
 'h'
 'e'
 's'
 'e'
 'n'

julia> join(res)
"fuadhesen"

Is this what you wanted?

EDIT Here is a merge example:

julia> str1 = "fuad"
"fuad"

julia> str2 = "hesen"
"hesen"

julia> str1, str2 = "fuad", "hesen"
("fuad", "hesen")

julia> c1, c2 = collect(str1), collect(str2)
(['f', 'u', 'a', 'd'], ['h', 'e', 's', 'e', 'n'])

julia> res = Char[]
0-element Array{Char,1}

julia> for i in 1:max(length(c1), length(c2))
           i > length(c1) || push!(res, c1[i])
           i > length(c2) || push!(res, c2[i])
       end

julia> res
9-element Array{Char,1}:
 'f'
 'h'
 'u'
 'e'
 'a'
 's'
 'd'
 'e'
 'n'

julia> join(res)
"fhueasden"
like image 117
Bogumił Kamiński Avatar answered Sep 18 '25 09:09

Bogumił Kamiński


There are many issues with your code. @BogumilKaminski has shown how you can concatenate strings in general, but you asked if what you "wrote is right", so I'll point out some problems.

str1 = "fuad"
str2 = "hesen"
result = ""
str = ""
merge = str1 * str2

As mentioned, strings are immutable, that means once you have made one you cannot change it, only make a new one. Above, you are initializing result to be an empty string--that won't work, you cannot update that. You're also initializing str, but that makes no sense either, because below you are reassigning it, so the initialization was wasted anyway. Notice also that str1 and str2 are of different lengths, which is a problem.

if str1>str2
str= str1
else str=str2
end

Here, you are comparing str1 and str2 with a >. I'm not sure what you are going for. This is a lexical comparison, so you are ordering them alphabetically. Is that what you wanted? Or did you want to compare their lengths? In that case you should write length(str1) > length(str2). If you actually wanted alphabetical comparison, you can instead write str = max(str1, str2).

for i = 1:merge
        result[i]=str1[i]
        result[j+1]=str2[j]
        j=j+1
        i=i+1
end

This won't work, for four reasons, or five reasons, depending on how you count.

  • 1:merge makes no sense, since merge is a string. You cannot count from 1 to "fuadhesen". (Also, you cannot use strings or characters as indices into an array.) I guess you meant for i = 1:length(merge), is that right?

  • Strings are immutable, so you cannot mutate/update them like this anyway.

  • Even if strings were mutable, you only pre-allocated empty strings, so writing result[i] = str1[i] would not be allowed because result is empty and would not have space to accept any characters anyway. If strings were mutable you would have to use push!.
  • The variable j just shows up in your loop without any initial value. Where did that come from?

Edit: BTW, here is a partial solution to your question of merging. You can use the zip function, which 'zips' two (or more) iterators together like a zipper. Then, use the append! function which adds multiple elements to the end of your collection (for single characters you can use push!). In the end, creates a string with join:

cstr = Char[]  # initialize empty vector of Characters
for c in zip(str1, str2)  # iterate over zipped strings
    append!(cstr, c)  # append them onto your empty array
end
# push!(cstr, last(str2))  # str1 and str2 are different lengths, if you want last 'n' to join you can uncomment this line.
str = join(cstr)  # collect into string

Please make sure that you read the documentation for strings, https://docs.julialang.org/en/v1/manual/strings/ Also look up the docs for push!, append!, zip and join. By just copying the code you won't learn much.

like image 36
DNF Avatar answered Sep 18 '25 09:09

DNF