jeudi 24 mai 2018

Beginner c# task error

so i am creating a program that will take in a string and output it as each character used and how many of that character there is in a row. For example "aaarrrgggghhhh" would output: a3r3g4h4. My current program has a bug where it wont output the last character, could anyone help me spot the error, thanks!

public static void Main()
{
    int count = 1;
    Console.Write(" Input a string : ");
    string str1 = Console.ReadLine();
    for (int i = 0; i < str1.Length-1; i++)
    {
        if (str1[i] == str1[i+1] )
        {
            count++;
        }
        else
        {
            Console.Write(Convert.ToString(str1[i]) + count);
            count = 1;
        }
    }
    Console.ReadKey();
}

Aucun commentaire:

Enregistrer un commentaire