count the number of times a letter 'e' occurred in a string

count the number of times a letter 'e' occurred in a string

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 7135 Views - Last Post: 29 March 2010 - 12:28 PM Rate Topic: -----

#1 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

count the number of times a letter 'e' occurred in a string

Posted 28 January 2010 - 08:43 PM

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

namespace Chapter_6___13
{
	class Program
	{
		static void Main(string[] args)
		{
			int a = 0;
			int x = 0;
			string s = Console.ReadLine();
			do
			{
				//finding the index of e
				x = s.IndexOf('e');
				if (x != -1)
					a++;
				s = s.Substring(x + 1);
			} while (x != -1);
			Console.WriteLine("number of letter e is : {0}", a);



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: count the number of times a letter 'e' occurred in a string

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: count the number of times a letter 'e' occurred in a string

Posted 28 January 2010 - 08:48 PM

Alright, a few things wrong with your post here.

1.) You were missing code tags, always use code tags around code.

2.) You posted a C# topic under the C/C++ forum

3.) You put the description into the sub title. Please post a detailed description of your question inside the post so we can know why we are looking at this code.

Thanks.
Was This Post Helpful? 1
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: count the number of times a letter 'e' occurred in a string

Posted 28 January 2010 - 09:00 PM

You can do something like this:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] items = Console.ReadLine().ToCharArray();
            
            int occurances = 0;

            foreach (char character in items)
            {
                if (character == 'e' || character == 'E')
                {
                    //you could do occurances++;
                    occurances = occurances + 1;
                }
            }

            Console.WriteLine("There were " + occurances.ToString() + " letters found.");
            Console.ReadLine();
        }
    }
}



This post has been edited by stapia.gutierrez: 28 January 2010 - 09:02 PM

Was This Post Helpful? 1
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 04:34 AM

View Poststapia.gutierrez, on 29 Jan, 2010 - 12:00 AM, said:

You can do something like this:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] items = Console.ReadLine().ToCharArray();
            
            int occurances = 0;

            foreach (char character in items)
            {
                if (character == 'e' || character == 'E')
                {
                    //you could do occurances++;
                    occurances = occurances + 1;
                }
            }

            Console.WriteLine("There were " + occurances.ToString() + " letters found.");
            Console.ReadLine();
        }
    }
}




You don't need to use a char array. A string can be enumerated over just like an array. This would be a bit more efficient because you wouldn't need to convert the incoming string from the ReadLine to an array. You would just leave it as a string and put it in the foreach loop.
Was This Post Helpful? 1
  • +
  • -

#5 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 09:21 AM

An if statement checking for any occurrences of 'E' might help also. Just remember to use .ToUpper().

Something like this:
string word=Console.ReadLine();

int occurrences=0;
if(word.ToUpper().Contains("E"))
{
	 foreach(char letter in word.ToUpper())
	 {
		  if(letter=='E') { occurrences++; }
	 }
}


This post has been edited by DuoShin85: 29 January 2010 - 09:23 AM

Was This Post Helpful? 1
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 10:23 AM

Here's a way to do it.

using System;
using System.Linq;

namespace CharacterCountDemo
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("E occurs {0} times.", GetCharacterOccurrenceCount("eTeheies is a test", 'e'));
			Console.WriteLine("E occurs {0} times.", GetCharacterOccurrenceCount("eTehEies is a tEst", 'e', SearchType.CaseSensitive));

			Console.WriteLine("X occurs {0} times.", GetCharacterOccurrenceCount("XxXxSxDDX xxxX 432", 'x'));
			Console.WriteLine("X occurs {0} times.", GetCharacterOccurrenceCount("XxXxSxDDX xxxX 432", 'x', SearchType.CaseSensitive));
			Console.ReadLine();
		}

		private static int GetCharacterOccurrenceCount(string input, char searchCharacter)
		{
			return GetCharacterOccurrenceCount(input, searchCharacter, SearchType.IgnoreCase);
		}

		private static int GetCharacterOccurrenceCount(string input, char searchCharacter, SearchType searchMethod)
		{
			switch (searchMethod)
			{
				case SearchType.CaseSensitive:
					return (from c in input
							where c == searchCharacter
							select c).Count();
				case SearchType.IgnoreCase:
					return (from c in input.ToUpper()
							where c.ToString().ToUpper() == searchCharacter.ToString().ToUpper()
							select c).Count();
				default:
					throw new ArgumentException("Provided search type is not valid.");

			}
			throw new ArgumentException();
		}

		public enum SearchType
		{
			CaseSensitive,
			IgnoreCase
		}
	}
}



This way the user has the option to specify case or not.

What's interesting though is that character cannot be pushed to Upper so you have to convert the character to string first.
If anyone knows a better way to do this, I'm all ears because I think this is ugly as hell:

where c.ToString().ToUpper() == searchCharacter.ToString().ToUpper()


Was This Post Helpful? 1
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 10:32 AM

That's some delicious spaghetti. :D
Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 12:02 PM

Quote

That's some delicious spaghetti.


Aww... ouch :(

Improvement suggestions are welcome.
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 12:15 PM

View PostMentalFloss, on 29 Jan, 2010 - 03:02 PM, said:

Quote

That's some delicious spaghetti.


Aww... ouch :(

Improvement suggestions are welcome.


just as personal preference, I have never been a fan of the methods that have one line that calls another method, excluding overloaded methods. Nothing wrong with your code though.

I would have probably just used a boolean to determine whether to ignore case or not.

This post has been edited by eclipsed4utoo: 29 January 2010 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 12:32 PM

View PostMentalFloss, on 29 Jan, 2010 - 11:02 AM, said:

Quote

That's some delicious spaghetti.


Aww... ouch :(

Improvement suggestions are welcome.



It's just a personal aesthetic thing. :P

I don't mind code being verbose if it promotes code readability.
Was This Post Helpful? 0
  • +
  • -

#11 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 12:38 PM

Quote

I would have probably just used a boolean to determine whether to ignore case or not.


I personally don't like boolean parameters for two reasons.
1. You are locked into two possible values. If requirements change, it would probably be easier to have had an enum that you can simply add a value to. For example, let's say that the character checker was only supposed to check if the first letter matches. I can add a new search type called FirstCharacter and then add logic to the method to handle that. With a boolean, I'd have to add a new parameter or something else.

2. It's hard to know at a glance what the boolean means at the calling code site. To illustrate, consider the following two calls:

DoSomethingMagical(true, false);



VS

DoSomethingMangical(MoonPhases.Ignore, PixieDust.Enabled)




EDIT:

Quote

just as personal preference, I have never been a fan of the methods that have one line that calls another method, excluding overloaded methods.


I did that as something to make the calling code easier to deal with. You can either go the default behavior or you can specify. If you don't need the extra control, that's fine. You just call that simpler method. Otherwise, you can control the behavior.

Internally, it's the same thing. The caller doesn't know that though.

This post has been edited by MentalFloss: 29 January 2010 - 12:40 PM

Was This Post Helpful? 1
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 12:40 PM

View PostMentalFloss, on 29 Jan, 2010 - 03:38 PM, said:

Quote

I would have probably just used a boolean to determine whether to ignore case or not.


I personally don't like boolean parameters for two reasons.
1. You are locked into two possible values. If requirements change, it would probably be easier to have had an enum that you can simply add a value to. For example, let's say that the character checker was only supposed to check if the first letter matches. I can add a new search type called FirstCharacter and then add logic to the method to handle that. With a boolean, I'd have to add a new parameter or something else.

2. It's hard to know at a glance what the boolean means at the calling code site. To illustrate, consider the following two calls:

DoSomethingMagical(true, false);



VS

DoSomethingMangical(MoonPhases.Ignore, PixieDust.Enabled)



I see your point.
Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 12:43 PM

I agree with your 'locked in a cage' point. Good stuff!

has018, enjoy your mind-food. :)
Was This Post Helpful? 0
  • +
  • -

#14 knichols  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 21
  • Joined: 09-November 09

Re: count the number of times a letter 'e' occurred in a string

Posted 29 January 2010 - 09:50 PM

Just do it in one line like this.


int Occurrences = Regex.Matches(s, "[e]", RegexOptions.IgnoreCase).Count;



Was This Post Helpful? 1
  • +
  • -

#15 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: count the number of times a letter 'e' occurred in a string

Posted 02 February 2010 - 05:25 PM

Thank you all
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2