13 Replies - 1712 Views - Last Post: 04 January 2012 - 07:23 AM Rate Topic: -----

#1 RobinGoodfellow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-January 12

Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 08:32 AM

I am writing a small program which will calculate the total number of Hash Tags from a text file. Once the text file has been loaded into my program I try to calculate the total number of times the # symbol appears using the following code which generates an error message;

foreach(char # in text)


I can use the foreach statement to calculate the number of S's or A's in the text but but not the number of #'s. Is this because the # symbol is not recognized as a Char Type by C#? Any help would be greatly appreciated, thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Using Foreach statement to read symbols from String?

#2 Gavisann  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 382
  • Joined: 01-July 11

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 08:37 AM

You can not name a variable '#'. Name the variable something else like 'pound' and your program should work fine.

example
static void Main()
        {
            string text = "my phone # is";
            foreach (char c in text)
            {
                Console.WriteLine(c);
            }
            Console.Read();
        }


Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 09:17 AM

It looks a lot like you missunderstand how that line of code works.

Quote

I can use the foreach statement to calculate the number of S's or A's in the text but but not the number of #'s.


foreach(char A in text) Does not count the number of time 'A' appears in text.
foreach(char S in text) Does not count the number of time 'S' appears in text.
foreach(char # in text) Does not count the number of time '#' appears in text.

If you tried to use it like this as your post suggests, there is no way it actually worked correctly.

What this tells me is that you are trying to develop a program from scratch without first learning how to code, or at least how to code in C#. That never works for anyone and we tell people a LOT that they need to learn to code first, through books and tutorials and lessons. *Then* start developing software from scratch.

Let me say this though... It sounds like this is all VERY new to you. This type of thing is covered very early on in any self-teaching book. Which makes me think you really aren't ready to be designing an application yet.

Newbie/Rookie said:

I have a little programming experience but I need to write ...


You need to start there. I can't say "I have little experience in speaking Russian, but I have been assigned to write a mystery novel in Russian. Can you help me?"

We can help you by saying "First learn basic programming and the language of C#. Then take on assignments." Could someone here write this program for you? Sure. Could someone here map out all the processes you need to follow and do the Software Design part of this in the slim hope you could code it from there? Sure. But we don't volunteer to do the job that you're either getting paid for, or getting a grade for. You may want to read this.

For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Dec 2011
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 RobinGoodfellow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-January 12

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 09:19 AM

Thank you Gavisann I appreciate your reply but I do not yet understand how to solve this problem. I understand that I cannot name a variable # but all I want is to simply calculate the number of times the # symbol appears in a string which I know for a fact will have any given number of # symbols. Can I not use the foreach statement to do this? If not might you advise me on the correct way of going about this. Once again I appreciate your help =]
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5630
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 09:38 AM

Re-read post # 2.

Gavisann already showed you how to loop through each character of a text string.
Try actually running that code so you can learn what it does.

From there you just have to add an "if" statement within the loop and increment a counter int every time the char is the char you are looking for.


You said you were successfully counting the number of 'S' or 'A' that appear in a text string. Can you show us that code that is working for you? We can show you what to change to make that work. But I suspect that code didn't really work as you thought it did and want to confirm that.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 09:38 AM

Yes, YOU can use a foreach statement do accomplish what you are trying to do. However, YOU will need to do that. We try to avoid giving you the answer as that will simply allow you to copy and paste and not understand what is occurring.

Gavisann's answer is perfect as it shows you exactly how to loop through a string variable and print out each character. Now, you aren't going to be printing them out, but using that code, you *should* be able to determine what you need to do next.
Was This Post Helpful? 0
  • +
  • -

#7 RobinGoodfellow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-January 12

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 09:39 AM

tlhInq 'toq I appreciate your reply. You are right that I am not very experienced with C#. I have had a few years experience programming with Basic languages however it probably will not surprise you to know that this is my very first delve into OOP. This ridiculously simple function I am trying to write is essential to the completion simple text analyzation program which has been set as an assignment by my university (I am in my first year and this is my first C# assignment). I have written the rest of the functions for this program already and believe it or not several of these functions require utilization of the foreach statement in it's correct usage. The reason I have tried to use the foreach statement as a solution to my # symbol problem is that at the time being I simply do not know any other way. I do indeed have reading materials at my disposal but they are relatively useless to me given that I do not know exactly what it is I am looking for in them. My assignment is due on the 6th and this is the only part of it I have struggled with thus far and ironically I saved this part for last as I thought it would be the easiest. Perhaps if you could just point me in the right direction? Even if you could suggest a keyword I could look up in the index section of the dozen c# programming bibles I have lying around the room I would appreciate it! Honestly I am in desperate need!
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 09:51 AM

There is no built-in way of doing what you are trying to do. YOU have to handle the logic yourself(which is what the assignment is teaching you). There is nothing for you to look up in a textbook or reference manual. You have been given code on how to loop through each character in a string. What more do you want?
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 10:19 AM

I'll give you a few hints.

foreach
if
char
string
++ operator

Read up on those links and you'll know everything you need to do this.
Was This Post Helpful? 0
  • +
  • -

#10 RobinGoodfellow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-January 12

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 10:31 AM

Thanks everyone for your help! In the end I simply used an Index.Of loop to find all instances of the # symbol in my string, no foreach required. =)
Was This Post Helpful? 0
  • +
  • -

#11 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 12:32 PM

Haha, hell of a way to make a glorious comeback! :D

On topic:

Are there any performance differences between the two solutions?

Solutions:

1. foreach with a counter
2. using IndexOf

This post has been edited by RexGrammer: 03 January 2012 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Using Foreach statement to read symbols from String?

Posted 03 January 2012 - 01:15 PM

The internal workings of IndexOf use some sort of loop. Therefore, using IndexOf in a loop means you're doing nested looping. Doing a foreach with a counter would be a single loop. That would be the "faster" way, but I doubt there would be any noticeable differences you could observe without a timer.
Was This Post Helpful? 0
  • +
  • -

#13 RobinGoodfellow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-January 12

Re: Using Foreach statement to read symbols from String?

Posted 04 January 2012 - 05:43 AM

int count = String.ToCharArray().Count(x => x == '#'); // Count number of Hashes


In one line haha ;-)
Was This Post Helpful? 1
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Using Foreach statement to read symbols from String?

Posted 04 January 2012 - 07:23 AM

Well, yeah, but we weren't going to just hand him copy/pastable code. For the record, you can skip the ToCharArray call and it'll still work, string is already an IEnumerable<char>.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1