Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.80/5 (4 votes)
See more:
if my string value is string value = "abcabcbcbc" i want to displlay like

a2b3c4
Posted
Comments
Tejas Prajapati 10-Feb-14 7:50am    
Check this Solution for Asp.Net: http://mrbool.com/how-to-count-char-word-and-line-in-a-textbox-using-asp-net-and-csharp/25288#

C#
using System.Linq;
class Program
{
    public static void Main(string[] args)
    {
        string s = "abcabcbcbc";
       char[] array =  s.Distinct().ToArray();
       string output = "";
       foreach (char c in array)
           output += c.ToString() + s.Count(k => k == c);

    }
}


or

C#
string s = "abcabcbcbc";
       string output = "";
       s.Distinct().ToList().ForEach(k => output += k.ToString() + s.Count(k0 => k0 == k));
 
Share this answer
 
v2
I found ( :-) ) this tip: "Counting Occurrences"[^] (It is NOT C# code, however the algorithm is explained).
 
Share this answer
 
Try this code:-

C#
string szInput = "abcabcbcbc";
            Dictionary<string, int> dic = new Dictionary<string, int>();
            foreach (char c in szInput)
            {
                if (dic.Keys.Contains(c.ToString()))
                {
                    dic[c.ToString()] = dic[c.ToString()] + 1;
                }
                else
                {
                    dic.Add(c.ToString(), 1);
                }
            }

            string szOutput = "";//ur result here
            foreach (string s in dic.Keys)
            {
                szOutput = szOutput + s + dic[s].ToString();
            }
 
Share this answer
 
Its solve your solutions.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestPRoject2
{
    class Program
    {
        static void Main(string[] args)
        {
            string teststring = "abcdefgha";
            foreach (char c in teststring)
            {
                int w = teststring.Count(x => x == c);
                Console.WriteLine(c+""+w);
            
            }
            Console.ReadKey();
        }
    }
}


run and you got your ans
 
Share this answer
 
Its a console Applications I think you can implement it now.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestPRoject2
{
    class Program
    {
        static void Main(string[] args)
        {
            string teststring = "abcdefgha";
            foreach (char c in teststring)
            {
                int w = teststring.Count(x => x == c);
                Console.Write(c+""+w);
            
            }
            Console.ReadKey();
        }
    }
}
 
Share this answer
 
v2
Comments
BillWoodruff 10-Feb-14 8:48am    
The task here is to calculate how many of each character are in the input string, not the position of a character in the input string.
[no name] 10-Feb-14 9:25am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestPRoject2
{
class Program
{
static void Main(string[] args)
{
string teststring = "abcdefgha";
foreach (char c in teststring)
{
int w = teststring.Count(x => x == c);
Console.Write(c+""+w);

}
Console.ReadKey();
}
}
}
BillWoodruff 10-Feb-14 11:49am    
There's nothing wrong with updating an existing solution. By the way, I did not down-vote you.
[no name] 10-Feb-14 12:31pm    
ok thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900