Note that the above solutions won't work if your input happens to be
non-ascii -- ggggg's code will give something like "3:3-5:2-4:�-�" as the
output for "abcabü".
Imho it's better to build the string you want in the end from scratch
instead of treating it as a mutable object:
input = "abcabü"
tmp = IOBuffer()
ind = 1
for char in input
if isodd(ind)
print(tmp, Int(char) + 2 - Int('a') + 1 )
ind == length(input) || print(tmp, ':')
else
print(tmp, Int(char) + 1 - Int('a') + 1)
ind == length(input) || print(tmp, '-')
end
ind += 1
end
out = takebuf_string(tmp)
This will give you a sensible result even for unicode input
("3:3-5:2-4:157" in the case I mentioned above).
Am Mittwoch, 24. August 2016 16:35:58 UTC+2 schrieb Rishabh Raghunath:
>
> *Hello Julia Users !!..*
>
> I have asked this question before in the forum but am still a bit unclear
> on how to do the below.. I have condensed a few of my doubts in the
> question below.
>
> Being from a C background where strings are just character Arrays,
> manipulating individual characters using the array index was easy.. However
> In julia that definitely does not seem to be the recommended way to go
> about doing things and I have a lot of doubts about the small details on
> the language. Here's a small example of what I am trying to achieve with my
> program .. This is a fairly simple program while doing it in C but I have
> absolutely no idea how to implement the same in Julia as strings cannot be
> treated like character arrays and i cannot just increment an index of the
> string to increase the ASCII value of the character in Julia. Please try to
> solve the problem or guide me on how to implement this in Julia.. I promise
> the question is very simple and isn't as complex as It may seem at a
> glance.
>
> *My Aim*:
>
> To write a program a program which accepts a string via the input
> and encrypts it to the following simple algorithm ..
>
> *Algorithm:*
>
> *Example Input String: abcabc*
>
> 1. Increase the ASCII value of the character at the *ODD index* of the
> string by 5 steps( example increased the ASCII value of ' *a* ' by 2
> thus making it ' * c* '
> 2. Increase the ASCII value of the character at the *EVEN index* of the
> string by 1 step( example increased the ASCII value of ' * b* ' by 1
> thus making it ' *c* '
> 3. Print the new encrypted string on Screen. I need it stored in a new
> string variable say z *(OUTPUT : ccebdd )*
> 3. Insert a ' - ' between each pair of characters of the string and store
> in new string in another string variable say y.
> 4. Print string y on the screen * (OUTPUT : cc-eb-dd ) *
> 5. Change all the characters in the string into its number equivalent. (
> a=1,b=2,c=3,.....) and separate the numbers representing each character by
> ' : '
> 6. Store the new string from the above operation in variable v and print v
> *(OUTPUT : 3:3-5:2-4:4)*
>
> Guys .. Try to help me out !!.. I tried searching everywhere on the
> internet for steps in doing the above operations in Julia but haven't found
> anything.. I always get the index errors for treating Julia the C way..
> Please try to create a program that does the above.. Just so that Its
> easier for me to understand what you are doing in the program rather than
> type the directives in English and then later misunderstanding stuff... I
> need to learn doing the above procedure the right way without messing with
> stuff like using indexes in strings for character manipulation that may get
> deprecated in the future..
>
> Thanks !! Waiting eagerly for a reply !!
>
>