0

"Deja Vu" task for C#

Input Format: A string of random letter characters (no numbers or other buttons were pressed). Output Format: A string that says 'Deja Vu' if any letter is repeated in the input string, or a string that says 'Unique' if there are no repeats. 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 w = Convert.ToString(Console.ReadLine()); string result = "Unique"; for (int i = 0; i == w.Length - 2 || result == "Deja Vu"; i++) { for (int j = 1; j == w.Length - i - 1; j++) { if(w[i] == w[i + j]) { result = "Deja Vu"; } else {continue;} } } Console.Write(result); } } }

30th Jan 2020, 1:43 AM
Golden Flower
Golden Flower - avatar
3 ответов
+ 1
Your inner loop is trying to check elements before i, so the initial conditions should be 0 for j and 1 for i. and the comparison should be w[j] == w[j+i] try using "sololearn" as example and go through the variable changes in each iteration and you can see
30th Jan 2020, 3:33 AM
Gordon
Gordon - avatar
+ 1
I finally made the correct code 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 w = Convert.ToString(Console.ReadLine()); string result = "Unique"; for (int i = 0; i < w.Length - 1; i++) { for (int j = 1; j < w.Length - i; j++) { if(w[i] == w[j + i]) { result = "Deja Vu"; } } } Console.Write(result); } } }
31st Jan 2020, 9:52 AM
Golden Flower
Golden Flower - avatar
0
I am noob, so I tried to understand why this won't work, but hopelessly. Doesn't for loop work like that?
30th Jan 2020, 1:47 AM
Golden Flower
Golden Flower - avatar