0

Hello we are doing this programming exercise: String doubles.

We would like to know with a detailed explanation (and more examples step by step if possible) why this code:

using System;
using System.Text;

public static class Kata
{
  public static string Doubles(string str)
  {  
    Console.WriteLine("str: "+str);
    for(int i=0; i<str.Length-1;i++){
      if(str[i]==str[i+1]){
        str=str.Remove(i--,2); //➖➖ operator does not store the modified i value inside i?
        Console.WriteLine("str: "+str);
      }
    }
    return str;
  }
}

Behaves differently than:

using System;
using System.Text;

public static class Kata
{
  public static string Doubles(string str)
  {  
    Console.WriteLine("str: "+str);
    for(int i=0; i<str.Length-1;i++){
      if(str[i]==str[i+1]){
        str=str.Remove(i,2); 
        Console.WriteLine("str: "+str);
        i-=1; //➖= operator DOES store the modified i value inside i?
      }
    }
    return str;
  }
}

We have read:

EDIT: For example with the following string:

zpezyhgwqevkekipkxboraceiehzpvajbjalaymsgvqlepelorhjgznaqtaghusuiypywzthoxasudhterdcwzvgyflmcgbjgfwqvefgmimdnzfsrgcpdqewohehvqlphqyzjvwpnbmpthydaiszcgnealtybeydagjvkndocgibgmxmnadtyxcynidjsxluzxelamjmpaxlfkjvsghmslztxuvevfymnawxwrtmnqinksdiopjwedqlnfqfqjblyfnlzanvybclifrmqfwczavthkdnvimztxucqjirndaediodolnylxkueowazqcdzilusmuimstcgvyrtolwnaxbmirxawfuwulawaeclrabiabhworlvpgrqnamqprwxmzegtrljvzwtlgkametzgnuzrlwawbwbdwgbkelrutpfgoerdmtrmjhlryouvrxkjixpyrpcpqkmfhvnajhfptpvklwcwewyrptmxrpjurafthzirncdtdachdgmtgqhqgtompqfzyuerjrolicsenpqetgedhxupmqsocbmnoebhdlcusbldyrcksjmcrofsyzjgpeqjniahdjlgmqrkoblsjdblmbhogkveotmtetjkfcqlokxudwnqxmcewmbmatosohbxyvxrjyxzuamxrmgyfbhcrhwxjloyxhizwizvhyocjvzeuonpnmazfqyezmpwdfprpyzjtinoxasnculkgitgjvwvjdrnsupdzpqesrlrsymljulsygxioemyzkxkiyvjihroxcnlikloqebavjgqamrusbtiqlzdajiuvdvcmkihcoseiwpxifwapmaxzmalvxqohazilysuvhodnphzfmuhskhqohikfbohgmjyhvrsjwzozgprjwnaioavklbnoyxejvqheypzblrzhcuoaozrmonvjgqwhaqvwuacrokinpekdiqnvwyieribtjpedqjmgznpalerexkdrbpdbkchrflzzayilnbrcqzfwmelrnufdtobvanohrmeswmxbadvoqdesajsrlgtesvcdlyetpztoqpvbifxyrhnepxzstxcfwvsxjqdlcapdawkrwmeupgiezcuxfvmlszjkvhufenpladmhwhwabcgloeqbrfqpelilrugypcrxevpmbyzljqtxwtozskzbzkpcucmeurauledwueforkpzcmjpjwbtwqixrhdpdeqwhjvwaqfljovqzhswuxpgrmnwnykchkjgeaunvloldyvsyjogvfojyjrijdcgahgkgsdscgbesnusruyekehbwzivilerihkwslmvdodkrlpzrofyafogkhkrckwkcxafisxoasxliwfjtpmjlownxfvylmltngirymgnbhvcynxnymzobnhdwzdohexclhjyiespfvhiwkoyvbhuqkecrbdgzcewznpqdzeuvlhmwoctzwxacghgampdrkuyuwxmyecsgdzkgquhbfqpnwsxujwevlftzbnmzhvsxkzdhqfyidsekydzrpiyxvxfkfdhitjxrdwopedxwlcuqpizpgkznegcfavabpuvmhbojnclsweponyiecmspsinzdcvknhfijoupcvzxbljypmibskriurigwogafoyheyetbhnvogypkrutscqwosfotombgvzjxzvzdchgdqonicxbtmtqznuxemzdpcjkvhbcgmkvbdsdnupgzvoxuqderbhdrpjqaexqjncyvbuwfruptxseuyzcvjoxldugxixzwlspbtwhyzmsicvlywkzqhstkypqbyugrvsiedcacsdvojibyxiptzfgfaeganjngutkdnjghtpthtmdofxywvndvbzvbnhoaklwcruxylklygafybxvnbkafgshgmsclbsrdgezxctfspauwaqmazalrvzmrlsiaenzngxodhgaodhmqslztuahaihsimjlencbgqcavmvseqhwefvawalsygfpjxgqyabgfapldgyhckalodrioecjcaknrygfujuhvyzuxeuqdvoxjcypgrxrdepctfztnsptqpdwxdgxpmwoytodgejdfirseaclfvhtzcsdkzcribdaqwnjpmjmuirolotojliclulasjbdnderbintwtmuzrkdswgnhwsbovsjxotzlqvnjtmkbpopqrvmtwdnwqlgqdwhzmtyjwmrickxafqpgxwyufcgqguxthpeimpwojnhbeijtknhbcqkorunidcoxamfpnpuawkpxqwsmkajkmgnoxbabjfmghwnqsutgesbnxuydsdluzortzvajkjpwythprgbunkaedgmjvmwvzedzgjsrdoefcevcmesfehtzrtxbocadtcmrzscvhmzuxylkwqtqrsqpfhrzubaceyksqufadiozctinexikmblwlsbcbfmxpynstbktnispbfeyqgxctumjgtjnzaguoqkdfqtcmpwdjvufncmvijdvlelzsxumbxbkvbsanuqexialydawolmfnorbetjoukclumjlhntzxdepxplwxgjrwtkbjngoqotnmdeymnroxesurwlmaymrpndrqkwpjmwjlvlmprbqjmbtnrcpkunqvuxhshugyilqbnocnimnhdidbrjgyqhurpmicnwurlrzd

We see:

enter image description here

Yone
  • 2,064
  • 5
  • 25
  • 56
  • For what value of `str` do they behave differently? – Sweeper May 10 '20 at 14:45
  • Which .NET implementations (.NET Framework, .NET Core,...) do you use? In Framework 4 - 4.8 in runs smoothly and the output of both Functions with your given input is the same. – CaTeR May 11 '20 at 16:40
  • to show you my code i used: https://dotnetfiddle.net/Usxucs maybe you watch a difference. – CaTeR May 11 '20 at 16:52

1 Answers1

0

In the first case, the value of i changes, but after we pass the current value to the Remove method, if we want to pass the new value to the Remove method, we put the decrement operator in front: --i or do it on a separate line:

 i--;
 str = str.Remove(i,2);