Whats wrong in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Whats wrong in this code?

Solving the Code Coach Problem - (Secret Message)! using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string input = Console.ReadLine(); char[] inputt = input.ToCharArray(); char[] letters = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; char[] rletters = new char[] {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'}; for(int i=0; i< inputt.Length; i++) { for(int a=0; a< letters.Length;a++) { if(inputt[i] == letters[a]) { inputt[i] = rletters[a]; } } } string output = new string(inputt); Console.WriteLine(output); } } }

2nd Jan 2020, 11:01 AM
Yash Chaudhari
Yash Chaudhari - avatar
4 Answers
+ 3
Try using break statement https://code.sololearn.com/cUN30h4jQSzP/?ref=app also you will still not able to achive all test cases in code coach because your code does not work for capital letters so I would suggest you to go for more optimised solution
2nd Jan 2020, 11:17 AM
Arsenic
Arsenic - avatar
+ 4
It is not working because You are not breaking the loop after you have changed the value of inputt[i] so after changing it the loop runs further and again encounters the same value whic was reversed and your function reverse it again
2nd Jan 2020, 11:14 AM
Arsenic
Arsenic - avatar
+ 2
Thanks Arsenic !
3rd Jan 2020, 6:51 AM
Yash Chaudhari
Yash Chaudhari - avatar
+ 2
I found a simple solution. Just convert user's input to lower case using .ToLower() and it works fine!
8th Jan 2020, 3:24 PM
Yash Chaudhari
Yash Chaudhari - avatar