Increment in ruby [solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Increment in ruby [solved]

The attached code is my attempt at secret message code coach problem. The program is supposed to take a string input in a. It then iterates for each character of a and checks if its a space, present in b or present in c. If its present in b or c, it is supposed to print the equivalent character of that position in d. d is the reverse of b and c in small letters. The problem here is that when it is iterating through b and c, it doesn't increase the value of count. So the answer is always a string of z's. The letter z is at index 0 in d, and the initial value of count is 0. That means that count is not incremented at all during the iteration. What is my error? Did I put the incrementation of count in the wrong place or what? I'm really confused as to why count's value isn't increasing https://code.sololearn.com/c3PZ84WqckTt/?ref=app

22nd Jan 2021, 8:30 AM
Sekiro
Sekiro - avatar
2 Answers
+ 3
Tarvs check the code Your if statement implementation as how it should be done in Ruby is wrong. you need to put end keyword before incrementing the count variable. edit: if(statement) ... end #CODE: #check out my question on this code #once the question is answered,the code goes a = gets.chomp b = "abcdefghijklmnopqrstuvwxyz" c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" d = "zyxwvutsrqponmlkjihgfedcba" a = a.split('') b = b.split('') c = c.split('') d = d.split('') for i in a if i==" " print " " else ct = 0 for j in b if i==j print d[ct] end ct += 1 end ct = 0 for k in c if i==k print d[ct] end ct += 1 end end end
22nd Jan 2021, 9:05 AM
Rohit
+ 3
RKK Thanks a lot man. Means a lot
22nd Jan 2021, 9:27 AM
Sekiro
Sekiro - avatar