+ 3

Code coach "camel and snake"

I can't for the life of me find a solution for this one. I tried both in python and c#, but it never manages to pass the final test which is locked, so I have no idea what I'm doing wrong!!

8th Sep 2025, 8:36 PM
Hiba
Hiba - avatar
12 Answers
+ 6
here's an alternative solution using more advanced methods. Also, C# code can now be written without the boilerplates. using System; string camel = Console.ReadLine(); string snake = camel.Substring(0,1).ToLower(); foreach(char c in camel.Substring(1)){ if(char.IsUpper(c)) snake +=
quot;_{char.ToLower(c)}"; else snake += c; } Console.WriteLine(snake);
9th Sep 2025, 1:21 PM
Bob_Li
Bob_Li - avatar
+ 3
Remove the unnecessary conditions from the inner if statement. if (i>0 && (char.IsLower(camel[i-1])||(i+1 < camel.Length && char.IsLower(camel[i+1])))) Becomes if (i>0) Then it will pass.
8th Sep 2025, 10:26 PM
Brian
Brian - avatar
+ 2
And we have no idea what code you tried. Show your code.
8th Sep 2025, 8:48 PM
Lisa
Lisa - avatar
+ 2
OMG it worked!! Thank you so much đŸ„°đŸ„°
8th Sep 2025, 11:22 PM
Hiba
Hiba - avatar
+ 1
Thank you! Didn't know about that, and it works great and it's simpler too. Thanks again 😊
9th Sep 2025, 3:49 PM
Hiba
Hiba - avatar
+ 1
camel = input() snake = "" for i in range(len(camel)): if i == 0: snake += camel[i].lower() elif 'A' <= camel[i] <= 'Z': snake += '_' + camel[i].lower() else: snake += camel[i] print(snake)
9th Sep 2025, 4:43 PM
Md Mehedi Hasan
Md Mehedi Hasan - avatar
+ 1
Oh this one is in python! It works better than mine, thank you so much 😊
9th Sep 2025, 5:22 PM
Hiba
Hiba - avatar
+ 1
Hiba here is a simpler method: just replace all upper cases with _char.ToLower() then trim the leading '_' if the first letter happened to be uppercase. using System; string snake = string.Empty; foreach(char c in Console.ReadLine()){ if(char.IsUpper(c)) snake +=
quot;_{char.ToLower(c)}"; else snake += c; } Console.WriteLine(snake.TrimStart('_')); I adapted it from my Python solution: print(''.join([f'_{c.lower()}' if c.isupper() else c for c in input()]).lstrip('_'))
9th Sep 2025, 10:51 PM
Bob_Li
Bob_Li - avatar
0
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 camel = Console.ReadLine(); string snake = ""; for (int i = 0; i< camel.Length; i++) { char c = camel[i]; if(char.IsUpper(c)) { if(i>0 && (char.IsLower(camel[i-1])||(i+1 < camel.Length && char.IsLower(camel[i+1])))) { snake += "_"; } snake += char.ToLower(c); } else { snake += c; } } Console.WriteLine(snake); } } }
8th Sep 2025, 9:13 PM
Hiba
Hiba - avatar
0
Here, it's correct tho. It just fails the final test
8th Sep 2025, 9:14 PM
Hiba
Hiba - avatar
0
Bob_Li It works great! Thank you so much for your help 😊
9th Sep 2025, 11:47 PM
Hiba
Hiba - avatar