Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
2.00/5 (8 votes)
See more:
Provide a method that when passed a string will return the count of every alpha character.

suppose if i take string=aabbc
o/p: a's=2
b's=2
c=1

has to come pls any one help me
Posted
Updated 20-Jan-11 10:35am
v2
Comments
[no name] 20-Jan-11 16:35pm    
We are not here to do your homework. Show what you have tried and ask a question about.
Sandeep Mewara 21-Jan-11 0:43am    
What kind of help? Have you made any effort from your side?
rupa1986 21-Jan-11 11:25am    
help insense i mean to get the best of all solutions not that im lazy to do more over its not my homework it is the question asked by an employer buddies:)can check my answer posted...
CPallini 21-Jan-11 12:38pm    
It is just a basic task, you should enjoy yourself finding your own solution.
R. Erasmus 24-Jan-11 2:06am    
I gave you a 5 for taking the initiative for seeking help on a public forum.
Its a good habit of getting into. But more so for a "if all else fails" matter. Hence the valid comments you got from everyone. ;P

If you have not studied dictionaries yet, recall that char values can be used as an array index (it's simply a number between 0 and char.MaxValue). If you create an array of counters, you'd be able to run through the characters in your string with a foreach loop, incrementing your counters along the way. Then you can print your result by going through the counters, and printing non-zero values (character code equals the index of the counter).

P.S. If you just need an answer, try this:
C#
var res = str.Distinct().ToDictionary(c => c, c => str.Where(a => a == c).Count());
It's a correct solution, but don't be surprised if your teacher suspects something ;)
 
Share this answer
 
Comments
Per Söderlund 25-Jan-11 3:02am    
Haha yeah,If you show a snippet with flawless lambda expressions your teacher might want you to write that again in front of him.
Here is a start to what you can do,
Its not complete and syntax might be lacking but I gotto work now.

void CountStringLetters(string message, list<char> charList, list<int> charCountList)
{
  int len = message.Length();
  char searchChar;
  int charCount;

  for (int i = 0; i < len; i++)
  {
    searchChar = message[i];
    
    for (int j = 0; j < len; j++)
    {
      if (message[j] == searchChar)
      {
        charCount = charCount + 1;
      }
    }

    charList.Add(message[j]);
    charCountList.Add(charCount);
    
    /* reset charCount */
    charCount = 0;
  }
}
Regards,
 
Share this answer
 
v3
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Assignment
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a character string");
            string input = Console.ReadLine();
            string output = CountAllCharecters(input);
            Console.WriteLine(output);
            Console.ReadLine();
        }
        static string CountAllCharecters(string strn)
        {
            string strresult = string.Empty;
            try
            {
                int i = 0;
                //strn = strn.Replace(" ", "");
                strn = Regex.Replace(strn, @"[\d\s@#%$^&*().,?!_]", "");
                i = strn.Length;

C#
while (i > 0)
                {
                    string substr = strn[0].ToString();
                    strn = strn.Replace(substr, "");
                    strresult = strresult + substr.ToUpper() + " occured " + (i - strn.Length).ToString() + " times \n";
                    i = strn.Length;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return strresult;
        }
    }
}
 
Share this answer
 
Comments
dasblinkenlight 21-Jan-11 12:49pm    
A good program most of the time makes it easy to answer a simple question: "what does this code do?" Trying to figure out the purpose of your piece of code is a puzzle in its own right.

You don't have to take my word for it: remove the words "occurred" and "times" from your code, show the result to your friends, and ask them to tell you what your code does.
Hello Rupa,
Your professor here. I gave the assignment to do it yourself, not to re-assign it to others. I need to see you in the classroom.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 16:40pm    
Right! And give him a final warning.
And don't forget to change the assignment.
(My 5)
Orcun Iyigun 22-Feb-11 15:09pm    
is this for real? if so thats a real fail for Rupa and he needs a lesson to learn.
Yusuf 22-Feb-11 15:25pm    
Smile. You are in candid camera. :-)
Create a
Dictionary<char,int>

Take a look at Char.IsLetter [^]

Loop over the characters in the string and update the dictionary, use the int as your character count.

Regards
Espen Harlinn
 
Share this answer
 
v2
Maybe you should ask your professor for help with your homework.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 16:38pm    
Some voters must say this answer is crazy? Shame on them! At least, this answer should teach them to be honest!
I vote 5 to oppose laziness and consumerism at the expense of more honest fellow students.

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