Monday, December 30, 2013

Consecutive Characters count

Complete the method to print the consecutive characters and the number of times appearing in a String.
public static void PrintConsecutiveCharacters(string input)
{ 
   //write code
}
Input & Output: 
UTC
Sample Input
Sample Output
01
“I saw a CD player and a modem in ccd”

CD 2
DE 1 
02
“Student List do not exist in system”


ST 4
DE 1
NO 1

SOLUTION: 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsecutiveCharacters_DEP
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the string: ");
            String str = Console.ReadLine();

            string str2 = str.ToUpper();
            char[] arr = str2.ToCharArray();
            
            int i = 0;
            SortedDictionary< string int="" > count = new System.Collections.Generic.SortedDictionary< string int="" >();
            char[] temp = new char[26];
            int j = 0;
            string te = "";
            bool flag = false;

            temp[j] = arr[i];
            i++;
            while (i < arr.Length)
            {
                if ((temp[j] + 0) == (arr[i] - 1))
                {
                    flag = true;
                    j++;
                    temp[j] = arr[i];

                    if (j > 0)
                    {
                        for (int k = 0; k < j; k++)
                        {
                            te = "";
                            for (int l = k; l <= j; l++)
                            {
                                te += temp[l];
                            }
                            if (count.ContainsKey(te))
                            {
                                int ct = count[te];
                                count[te] = ct + 1;
                            }
                            else
                            {
                                count.Add(te, 1);
                                //j = 0;
                            }
                        }
                        flag = false;
                    }
                }

                else if (j > 0 && flag == true)
                {
                    te = "";
                    for (int k = 0; k <= j; k++)
                    {
                        te += temp[k];
                    }

                    if (count.ContainsKey(te))
                    {
                        int ct = count[te];
                        count[te] = ct + 1;
                    }
                    else
                    {
                        count.Add(te, 1);
                        j = 0;
                        temp[j] = arr[i];
                    }
                    flag = false;
                }

                else
                {
                    j = 0;
                    temp[j] = arr[i];
                }
                i++;
            }

            //Printing ...
            foreach (var item in count)
            {
                Console.WriteLine(item.Key + "\t" + item.Value);
            }
        }
    }
}

No comments:

Post a Comment