How to Count no of repeated character in a String?



How to Count no of repeated character in a String?
Input   : ababcgah
OutPut : a3b2g1h1

Input : shivKvish
Output : s2h2i2v2k1

static void FindCharinString(string arr)
        {
{
           
          
            char[] chr = arr.ToCharArray();
            Array.Sort(chr); //SORTING ARRAY
            string newstr = "";
            for (int i = 0; i < chr.Length - 1; i++)
            {
                int count = 1;
                for (int j = i + 1; j <= arr.Length - 1; j++)
                {
                    if (chr[i] == chr[j])
                    {
                        count=count+1;

                    }
                    if (chr[i] != chr[j])
                    {
                        newstr += chr[i].ToString() + count;
                        i = j-1;
                        break;
                    }

                }
            }
            Console.WriteLine(newstr);
           


        }
  Output : K1h2i2s2




Share:

1 comment:

  1. I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing. this

    ReplyDelete

Popular

Tags

Mobile

Recent Posts