Home > Software engineering >  Replace each occurance from a string with a number c#
Replace each occurance from a string with a number c#

Time:04-01

how can I replace each occurrence of a char from a string with a number starting from 0.
e.g

aab 
001

tta
001

abba
0110 

and so on I m only allowed to use : using system

CodePudding user response:

string input = "abba";
string chars = string.Empty;
string result = string.Empty;

for (int i = 0; i < input.Length; i  )
{
    int index = chars.IndexOf(input[i]);

    if (index == -1)
    {
        chars  = input[i];
        index = chars.Length - 1;
    }

    result  = index.ToString();
}
  • Related