View difference between Paste ID: 4TGHimKr and ujM65epr
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
namespace _23.ReplaceConsecutiveIdenticalLetters
8
{
9
    class ReplaceConsecutiveIdenticalLetters
10
    {
11
        /* Write a program that reads a string from the 
12
         * console and replaces all series of consecutive 
13
         * identical letters with a single one. Example: 
14
         * "aaaaabbbbbcdddeeeedssaa"  "abcdedsa".*/
15
16
17
        static List<char> chars = new List<char>();
18
        static List<int> counts = new List<int>();
19
20
21
        static void Main()
22
        {
23
            // Console.WriteLine("Please enter sequence of chars:");
24-
            string inputString = "aaaAaabbbb bcdddee@# eed125 ssaat";// Console.ReadLine();
24+
            string inputString = "aaaaabbbbbcdddeeeedssaat";// "aaatAaabbbb bcdddee@# eed125 ssaatt";// Console.ReadLine();
25
26
            string letters = "abcdefghijklmnopqrstuvwxyzABCDEFJHIGKLMNOPQRSTUVWXYZ";
27
28-
            int count = 1;
28+
            int count;
29
30
            Console.WriteLine("char-->count");
31-
            for (int i = 0; i < inputString.Length - 1; i++)
31+
            for (int i = 0; i < inputString.Length; i++)
32
            {
33-
                if (letters.Contains(inputString[i]))
33+
                char currentChar = inputString[i];
34
                count = 1;
35-
                    if (inputString[i] == inputString[i + 1])
35+
                if (letters.Contains(currentChar))
36
                {
37-
                        count++;
37+
                    if (i == inputString.Length - 1)
38
                    {
39
                        if (currentChar != inputString[inputString.Length - 2])
40
                        {
41-
                        AddCharAndCount(inputString[i], count);
41+
                            AddCharAndCount(currentChar, 1);
42-
                        PrintCharToCount(inputString[i], count);
42+
                            PrintCharToCount(currentChar, 1);
43-
                        count = 1;
43+
                        }
44
                        else
45
                        {
46
                            break;
47
                        }
48
                    }
49
                    else
50
                    {
51
                        for (int j = i + 1; j < inputString.Length; j++)
52
                        {
53
                            char currentY = inputString[j];
54
                            if (currentChar == inputString[j])
55
                            {
56
                                count++;
57
                            }
58
                            else
59-
        private static void AddCharAndCount(char charOfString, int count)
59+
                            {
60
                                AddCharAndCount(currentChar, count);
61-
            chars.Add(charOfString);
61+
                                PrintCharToCount(currentChar, count);
62
                                i = j - 1;
63
                                count = 1;
64
                                break;
65
                            }
66
                            if (j == inputString.Length - 1)
67
                            {
68
                                AddCharAndCount(currentChar, count);
69
                                PrintCharToCount(currentChar, count);
70
                                i = j - 1;
71
                            }
72
                        }
73
                    }
74
                }
75
            }
76
77
            Console.WriteLine();
78
            Console.WriteLine("or");
79
            StringBuilder builder = new StringBuilder();
80
            for (int i = 0; i < chars.Count; i++)
81
            {
82
                builder.Append(chars[i]);
83
                Console.WriteLine("{0} --> {1}", chars[i], counts[i]);
84
            }
85
            Console.WriteLine(builder.ToString());
86
        }
87
88
        private static void AddCharAndCount(char currentChar, int count)
89
        {
90
            chars.Add(currentChar);
91
            counts.Add(count);
92
        }
93
        static void PrintCharToCount(char charOfString, int count)
94
        {
95
            Console.WriteLine("{0} --> {1}", charOfString, count);
96
        }
97
    }
98
}