0

pig latin Code gets skipped? C#

I programmed a code for the pig latin puzzle but the while loop gets skipped(?) and it just outputs the original sentence... Here's the 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 sentence = Console.ReadLine(); int i = 0; char letter; sentence += " "; while(i <= sentence.Length) { letter = sentence[i]; sentence.Remove(i, 1); while(sentence[i] != ' ') { i++; } sentence.Insert(i, letter + "ay"); i += 4; } Console.Write(sentence); } } }

8th Feb 2020, 12:59 PM
Drahnoel
Drahnoel - avatar
3 Réponses
+ 2
The while loop gets not skipped. while(i < sentence.Length) The last index of a string is length - 1. So i should not get = length. .Remove() & .Insert() return a String: sentence = sentence.Remove() same for Insert()
8th Feb 2020, 1:09 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Tanks a lot!
8th Feb 2020, 1:10 PM
Drahnoel
Drahnoel - avatar
+ 1
Drahnoel Your welcome :)
8th Feb 2020, 1:11 PM
Denise Roßberg
Denise Roßberg - avatar