Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I have wrote this program in c# but it doesn't work and i dont know which part i did wrong. The program supposed to return number of times each letter of alphabet occurs.it works but not with the correct answer.please help me through this.


C#
namespace letterss
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count = 0;
            char c = 'A';
            int m = (int)c;
            char[] s = new char[50];
            string t = textBox1.Text;
            for (int j = 0; j < t.Length; j++)
                s[j] = t[j];


            for (int n = 0; n < 25; n++)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] == c)
                        count++;


                }
                label1.Text = "Letter" + c + " occured " + count.ToString() + " times";


                int l = (int)c + n;
                c = (char)l;
            }
        }
    }
}


[EDIT: C# Code]
Posted
Updated 12-Dec-17 2:02am
v4

Here is a working console Application.

C#
class Program
    {
        static void Main(string[] args)
        {
            string inPutString = "CodeProject Quick Answers";
            char letterToBeChecked = 'e';
            var result = GetLetterOccurence(inPutString, letterToBeChecked);
            Console.WriteLine(" The occurence of letter '{0}' from the string {1} is {2}", 
                letterToBeChecked, 
                inPutString, 
                result);
            Console.WriteLine("\nPress any key to exit");
            Console.ReadLine();
        }

        /// <summary>
        /// Get the number of occurence of letter
        /// </summary>
        /// <param name="inPutString">InputString value</param>
        /// <param name="letterToBeChecked">LetterToBeChecked value</param>
        /// <returns></returns>
        private static int GetLetterOccurence(string inPutString, char letterToBeChecked)
        {
            char[] chars = inPutString.ToArray();  // Get the Characters(letters) from string

            var result = (from c in chars
                          where c.Equals(letterToBeChecked)
                          select c).Count();
            return result;
        }
    }


Output will be

The occurence of letter 'e' from the string CodeProject Quick Answers is 3

Good luck.
 
Share this answer
 
v3
Comments
Andy411 22-Mar-12 2:47am    
I like this solution, because you used Linq :-)
Wonde Tadesse 22-Mar-12 19:55pm    
Thanks
gggustafson 14-Apr-14 14:07pm    
I do not like this solution because it used Linq!
Wonde Tadesse 14-Apr-14 21:58pm    
Thanks.
VJ Reddy 23-Apr-12 19:29pm    
Good answer. 5!
You are searching twice for "A"

C#
for (int n = 0; n < 25; n++)
{
    for (int i = 0; i < s.Length; i++)
    {
       if (s[i] == c)
          count++;


    }
    label1.Text = "Letter" + c + " occured " + count.ToString() + " times";


    int l = (int)c + n; // in first round you will add 0, 
                        // so you are searching for 'A' again. And you will 
                        // never search for 'Z'
    c = (char)l;
}



C#
char[] s = new char[50];
string t = textBox1.Text;
for (int j = 0; j < t.Length; j++) // why are you coping here? it will crash if t.Length > 50
   s[j] = t[j];



Try something like this:

C#
static int foo(string source, char charToCount)
{
    int occurences = 0;

    for (int i = 0; i < source.Length; i++)
    {
        if (source[i] == charToCount)
            occurences++;
    }

    return occurences;
}


It's like solution 1 without Linq
Disadvantage of both solutions is, that you have to parse the whole string for every letter.
 
Share this answer
 
Comments
Wonde Tadesse 22-Mar-12 19:56pm    
5+
Andy411 23-Mar-12 2:53am    
Thanks, too :)
"It works but not with the correct answer" I would say the function does not work in that case.

Here is a hint. What if the text is lower case?

You will also have problems if the text is longer than 50 characters. It is also not necessary to create a char array from the string since a string is already an array of chars.
 
Share this answer
 
You want to look for the number of each letter, not one in particular. Process the input only once like this:
C#
void RespondToUserAction()
{
    Dictionary<char, int> histogram = Histogramify(TextBox1.Text);

    foreach (char oneChar in histogram.Keys)
    {
        TextBox2.AppendText(
            Environment.NewLine + oneChar + "\t: " + histogram[oneChar].ToString() + "x"
        );
    }
}

Dictionary<char,int> Histogramify(string input)
{
    Dictionary<char, int> histogram = new Dictionary<char, int>();

    foreach (char oneChar in input)
    {
        if (histogram.Keys.Contains(oneChar))
        {
            histogram[oneChar]++;
        }
        else
        {
            histogram.Add(oneChar, 1);
        }
    }

    return (histogram);
}
(this is untested)
 
Share this answer
 
Comments
Richard MacCutchan 14-Apr-14 6:12am    
Please don't waste time posting answers to questions as old as this.

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